60 likes | 201 Views
THE COMPUTE STATEMENT. Purpose: performs mathematical calculations Syntax Definition : COMPUTE {data-name [ROUNDED]}… = arithmetic-expression [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-COMPUTE] Symbol Reminder: + - * / ** also ( )
E N D
THE COMPUTE STATEMENT Purpose: performs mathematical calculations Syntax Definition: COMPUTE {data-name [ROUNDED]}… = arithmetic-expression [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-COMPUTE] Symbol Reminder: + - * / ** also ( ) Examples: COMPUTE total-cost = total-cost + meal-cost compute num-meals = num-meals + 1 compute tot-meal-cost = meal-cost * num-cust compute a = b + c / d For Your Information This statement calculates the arithmetic expression to the right of the = sign and stores the result in the variable(s) to the left of the = sign ALL variables in the arithmetic expression must be numeric items (i.e. not edited) The result variable (i.e. data-name) can be numeric or numeric-edited The ON SIZE ERROR is executed if the result is larger than the receiving field *** SUBTRACTION ISSUE! Arithmetic Syntax
THE ARITHMETIC EXPRESSION • Symbol Reminder: + - * / ** also ( ) • Precedence: just like evaluating math problems • () inside/out • ** from right to left • * / from left to right • + - from left to right • Examples: a=10, b=5, c=3, d=2, e = 4 • Compute x = a / b * c • Compute y = b + a – d / d ** c • Compute z = a / d – e / d * c For Your Information MOVE rules apply here when storing the arithmetic expression into the result memory location. Put in parenthesis and/or take out parenthesis to determine if the equations is still evaluated the same way. Don’t forget that each variable to the right of the = sign must have a value prior to the execution of the statement Watch out for divide by 0 error… is a run error! Arithmetic Syntax
THE ADD STATEMENT Syntax Definition: FORMAT 1: ADD {identifier-1 | literal-1} ... TO {identifier-2 [ROUNDED]}... FORMAT 2: ADD {identifier-1 | literal-1} ... TO {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-ADD] Examples: a=1, b=2, c=3, d=4 Add a b 3 TO c d Add a b 3 to d giving e Add a d giving c • For Your Information • All literals and fields that are part of the addition must be numeric. • The field after the word GIVING may be edited. • The result field, following the word TO or the word GIVING, must be a data-name, not a literal. • The result field is the only field changed as a result of the ADD operation. • When using the TO format, the contents of the receiving field are part of the ADD operation and are added along with the other fields. • When using the GIVING format, the result field is NOT part of the ADD operation • Notice in Format 2 that the TO is not required. • In Format 2, notice that the value after TO is singular • FYI: Example 2 is only allowed by the COBOL 85 compiler. • FYI: Convert to equivalent COMPUTE statements Arithmetic Syntax
THE SUBTRACT STATEMENT Syntax Definition: FORMAT 1: SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 [ROUNDED]}... FORMAT 2: SUBTRACT {identifier-1 | literal-1} ... FROM {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-SUBTRACT] Examples: a=1, b=2, c=4, d=8 Subtract a b from d. Subtract a b from c giving d. Subtract a b from c d For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. All the fields and literals preceding the word FROM are ADDED together and the sum is subtracted from the field following the word FROM. When using the GIVING format, the result of the subtraction is placed in the field following the word GIVING. The initial contents of the receiving field do not take part in the arithmetic operation. Arithmetic Syntax
THE MULTIPLY STATEMENT Syntax Definitions: FORMAT 1: MULTIPLY {identifier-1 | literal-1} BY {identifier-2 [ROUNDED]}… FORMAT 2: MULTIPLY {identifier-l | literal-l} BY {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]} ... [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-MULTIPLY] Examples: a=1, b=2, c=4, d=8 Multiply b by c d. Multiply b by c giving d For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. Make sure the receiving field is large enough to hold the result. Arithmetic Syntax
THE DIVIDE STATEMENT For Your Information The receiving field after the word GIVING may be edited. The receiving field must be a data-name, not a literal. The BY option must have a GIVING (see Format 1). The REMAINDER clause can be used to store the integer remainder of a division operation. The use of the REMAINDER clause is optional and does not alter the results of the original DIVIDE operation. The On Size Error clause applies to both the quotient and the remainder fields. Syntax Definitions: FORMAT 1: DIVIDE {identifier-1 | literal-1} INTO {identifier-2 [ROUNDED]}… FORMAT 2: DIVIDE {identifier-1 | literal-1} {INTO | BY} {identifier-2 | literal-2} GIVING {identifier-3 [ROUNDED]}... FORMAT 3: DIVIDE {identifier-l/literal-l} {INTO | BY} {identifier-2/literal-2} GIVING {identifier-3 [ROUNDED] [REMAINDER identifier-4]…} [ON SIZE ERROR stmt-group1] [NOT ON SIZE ERROR stmt-group2] [END-DIVIDE] Examples: a=1, b=2, c=4, d=8 Divide b into c d. Divide c into b. Divide c by b giving b. Divide b into 9 giving y remainder z. Arithmetic Syntax