Cobol 문법 (8) : Sum-up-02

yoneeki·2023년 8월 23일

cobolGrmr

목록 보기
8/11

ADD statement

PROCEDURE DIVISION.

ADD A TO B.		*> COMPUTE B = B + A
ADD 1 TO B.		*> COMPUTE B = B + 1
ADD A TO B GIVING C. 		*> COMPUTE C = A + B
ADD A B C GIVING D. 		*> COMPUTE D = A + B + C
  • We can use ON SIZE ERROR and ROUNDED on our results.
ADD A TO B ROUNDED			*> COMPUTE B ROUNDED = B + A
ADD A TO B ON SIZE ERROR ON SIZE ERROR DISPLAY "ERROR"	
*> COMPUTE B = B + A ON SIZE ERROR DISPLAY "ERROR"

SUBTRACT statement

SUBTRACT numbera from numberb
SUBTRACT numbera numberb numberc from numberd
SUBTRACT numbera from numberb GIVING numberc
  • COMPUTE can be used for subtraction.

MULTIPLY statement

MULTIPLY numbera BY numberb.
MULRIPLY numbera BY numberb GIVING numberc.
  • COMPUTE can be used for Multiplication.
  • We can also give ON SIZE ERROR and NOT ON SIZE ERROR.
  • We can also used ROUNDED.
MULTIPLY numbera BY numberd
	ON SIZE ERROR DISPLAY "THERE IS AN ERROR."
    NOT ON SIZE ERROR DISPLAY "THERE IS NO SIZE ERROR."

DIVIDE statement

DIVIDE numbera INTO numberb
DIVIDE numbera BY numberb
DIVIDE numbera BY numberb GIVING numberc
DIVIDE numbera BY numberb GIVING numberc REMAINDER variablename
  • COMPUTE can be used for DIVISION.
  • We can also give ON SIZE ERROR and NOT ON SIZE ERROR.
  • We can also use ROUNDED
DIVIDE numbera BY numberd
ON SIZE ERROR DISPLAY "THERE IS AN ERROR"
NOT ON SIZE ERROR DISPLAY "THERE IS NO SIZE ERROR"

COMPUTE statement

PROCEDURE DIVISION.
		COMPUTE B = B + A
        COMPUTE B = B - 1
        COMPUTE C = A * B
        COMPUTE D = A / B
        COMPUTE D = A ** B
  • We can use ON SIZE ERROR and ROUNDED on our results.
  • All variables should be numeric.
COMPUTE B ROUNDED B + A
COMPUTE B = B + A ON SIZE ERROR DISPLAY "ERROR"

Order of precedence in mathematical equations

COMPUTE ANSWER = 5 + 3 - 2 ** 2 * 3 / 2 / 1 ** 3
		ANSWER = 5 + 3 - 4 * 3 / 2 / 1
        ANSWER 5 + 3 - 6
        ANSWER = 2
  • At first step exponentiation( * * ) is done from left to right
  • In second step multiplication( * ) and division(/) is done from left to right
  • In third step addition(+) and subtraction(-) is done from left to right
  • If a parenthesis exists then everything in the bracket is solved first.
ANSWER = (5 + 3) - 2 ** (2 * 3 / 2 / 1) ** 3

Functions in COBOL

  • There are over 42 inbuilt functions in COBOL.

  • They are also called intrinsic functions.

  • These were added in 1989 in COBOL-85 standard. These are of 6 types.

    1. Mathematical : SUM, SQRT..
    2. Statistical : MEAN, MAX..
    3. Character : REVERSE, LENGTH..
    4. Date : CURRENT-DATE..
    5. Financial : PRESENT-VALUE..
    6. Trigonometric : SIN, COS..
  • Syntax to use a function is

1) FUNCTION func_name (args..) 

2) Ex. FUNCTION SUM(35, 21) or FUNCTION SUM(35 21)
	   FUNCTION MAX(1, 2, 3, 4) OR FUNCTION MAX(1 2 3 4)
  • Functions can be used within a COMPUTE statement.

Mathematical Functions

  • These perform Mathematical operation on the arguments.
    1. SUM
    2. SQRT
    3. REM
    4. MOD
    5. FACTORIAL
    6. LOG
    7. LOG10
    8. INTEGER
    9. INTEGER-PART
    10. NUMVAL
    11. NUMVAL-C
    12. RANDOM

Statistical Functions

  • These perform STASTICAL operation on the arguments.

    MEAN : Finds the average of the numbers.
    MEDIAN : Finds the number that lies in the middle of the values.
    STANDARD-DEVIATION : Finds how much the numbers vary from the mean.
    VARIANCE : Finds how much the numbers vary from the mean.
    RANGE : Difference of the Maximum and Minimum number.
    MID-RANGE : Mean of the Maximum and Minimum number.
    MAX : Maximum of all the numbers.
    MIN : Minimum of all the numbers.
    ORD-MAX : Position of the maximum number.
    ORD-MIN : Position of the minimum number.

Character Functions

LENGTH : Finds the length of the argument
REVERSE : Reverses the argument
UPPER-CASE : Upper-case all the characters in the argument
LOWER-CASE : lower-case all the characters in the argument
ORD : Finds the ordinal position of the collating sequence
CHAR : Finds the character based on the ordinal position

Financial Functions

ANNUITY

FUNCTION ANNUITY (interest-rate number-of-periods)

  • Used to find the installment value for the loan

PRESENT-VALUE

FUNCTION PRESENT-VALUE (interest-rate payment-value1 payment-value2)

  • Used to find the Present value for the series of future payments

Trigonometric Functions

SIN : calculates the sine
COS : calculates the cosine
TAN : calculates the tangent
ASIN : calculates the arcsine
ACOS : calculates the arccosine
ATAN : calculates the arctangent

  • Arguments must be in radians.

IF ELSE loop

	IF condition
    	..........
        ..........
    ELSE
    	..........
        ..........
    END-IF
  • Used to execute a set of statements if a condition is true.
  • Else will get executed when the condition is false.
  • End-if is used to denote the end of the IF ELSE statement.
  • Else and End-if are optional.
  • Conditions can be >, <, =, <=, >=, NOT

Nested IF statements

  • IF statements can be nested.
IF AGE = 18 
	DISPLAY "YOU ARE 18 YRS OLD"
ELSE IF AGE = 19
	DISPLAY "YOU ARE 19 YRS OLD"
ELSE IF AGE = 20
	DISPLAY "YOU ARE 20 YRS OLD"
IF GENDER = MALE
	DISPLAY "YOU'RE NOT ELIGIBLE TO PARTICIPATE"
ELSE 
	IF AGE > 18
    	DISPLAY "YOU ARE ELIGIBLE TO PARTICIPATE"
    ELSE 
    	DISPLAY "YOU ARE NOT ELIGIBLE TO PARTICIPATE"
    END-IF
END-IF

PERFORM UNTIL statement

PERFORM procedure-name UNTIL condition
	.............
    ...............
STOP RUN.
  • Used to execute a procedure in a loop.
  • Control is passed to the procedure named in the PERFORM statement.
  • After all the statments in the PROCEDURE are executed then the condition is checked again. if the condition is still false then the statements will get executed again.
  • The procedure keeps on executing until the condition becomes true.

PERFORM with TEST AFTER

PERFORM DISPLAY-PARA
			WITH TEST AFTER
            UNTIL I > 5
            
DISPLA-PARA.
	DISPLAY "HELLO WORLD"
    COMPUTE I = I + 1.
  • The default option is WITH TEST BEFORE.

PERFORM with VARYING clause

PERFORM COMPOUND-INTEREST-CALCULATION
	VARYING YEARS FROM 1 BY 1 
    		UNTIL YEARS > 5

PERFORM with TIMES clause

  • PERFORM can be executed multiple times using TIMES clause.
PERFORM COMPOUND-INTEREST-CALCULATION 5 TIMES.
PERFORM COMPOUND-INTEREST-CALCULATION NO-OF-YEARS TIMES.
PERFORM 100-DISPLAY-HELLO 20 TIMES.
STOP RUN.

100-DISPLAY-HELLO.
	DISPLAY "Hello World!".

EVALUATE statement

EVALUATE AGE
		WHEN 15	DISPLAY	"YOU ARE 15"
        WHEN 16 DISPLAY "YOU ARE 16"
        WHEN 17 DISPLAY "YOU ARE 17"
        WHEN 18 DISPLAY "YOU ARE 18"
        WHEN OTHER DISPLAY "YOU ARE NONE OF THE ABOVE"
END-EVALUATE.
EVALUATE TRUE
	WHEN AGE > 18 DISPLAY "YOU ARE AN ADULT"
END-EVALUATE.
EVALUATE GENDER ALSO AGE > 18
	WHEN "FEMALE" ALSO TRUE DISPLAY "YOU ARE ELIGIBLE FOR CONTEST"
END-EVALUATE.
  • The conditions are checked in sequence.
  • Once the condition becomes true the EVALUATE is exited.

REFERENCE Modification

MOVE FULL-DATE(3:2) TO MONTH
MOVE FULL-DATE(5:4) TO YEAR
  • Reference modification can be used to refer a portion of a field.
  • Syntax is ... Fieldname(offset : Length)
  • If length is omitted then all the characters after offset are selected.

STRING Statement

STRING FIRST-NAME DELIMITED BY SIZE
	   LAST-NAME DELIMITED BY SIZE
       INTO FULL-NAME
       
       WITH POINTER POINTER-FIELD
       
       ON OVERFLOW DISPLAY "OVERFLOW IS THERE"
       NOT ON OVERFLOW DISPLAY "NO OVERFLOW IS THREE"
  • Used to concatenate two or more fields together.
  • Delimit by size will send the whole field.
  • Pointer clause determines the starting position in the receiving field.
  • Overflow clause will get executed when more characters are sent to the receiving field that it can hold.

UNSTRING statement

UNSTRING FULL-NAME DELIMITED BY ""
		 INTO FIRST-NAME MIDDLE-NAME LAST-NAME
         
         TALLYING IN COUNTER1
         WITH POINTER POINTER-FIELD
         ON OVERFLOW DISPLAY "OVERFLOW IS THERE"
         NOT ON OVERFLOW DISPLAY "NO OVERFLOW IS THERE"
  • Used to divide a field into two or more field.
  • Pointer clause determines the starting position in the sending field.
  • Overflow clause will get executed when more characters are sent to the receiving field than it can hold.
  • TALLYING counts the number of receiving fields.

INSPECT statement

INSPECT FULL-NAME
	TALLYING COUNTER1 FOR CHARACTERS BEFORE SPACE.
  • INSPECT can be used to count, replace and convert characters in a string.
INSPECT FULL-NAME 
	TALLYING COUNTER1 FOR ALL ",".
    
INSPECT FULL-NAME 
	TALLYING COUNTER1 FOR LEADING "*".
    
INSPECT FULL-NAME
	TALLYING COUNTER1 FOR ALL "*" BEFORE ".".

INSPECT with REPLACE clause

INSPECT FULL-NAME 
	REPLACING ALL "," BY "".
  • Replace can replace characters in a string.
INSPECT FULL-NAME
	REPLACING CHARACTERS BY "0" AFTER ".".

INSPECT FULL-NAME
	REPLACING LEADING "*" BY ZERO.
    
INSPECT FULL-NAME
	REPLACING ALL "CR" BY " ".

INSPECT with CONVERTING clause

INSPECT FULL-NAME
	CONVERTING "," TO " ".
    
INSPECT FULL-NAME
	REPLACING ALL "," BY " ".
    
INSPECT FULL-NAME
	CONVERTING "ABCD" TO "abcd".
profile
Working Abroad ...

0개의 댓글