3.1.
Cobol condition statements
A condition causes the object program to select between alternative paths of control, depending upon the truth value of
a test.
There are different types of conditions available. Some of them are
• Relational
• Sign
• Class
• Negated simple
• Compound
• Condition names
We will look into each of the conditions with an example.
1. Relational
his is used for the comparison of numeric operands.
The various relational operators that can be used are
– > GREATER THAN
– < LESS THAN
– = EQUAL TO
– NOT
Syntax: Operand-1 RELATIONAL OPERATOR Operand-2.
Eg: IF A > B, IF A < B etc
2. Sign
Determines whether or not the algebraic value of an operand is either positive, negative or zero
Operand can be numeric identifier or an algebraic / arithmetic expression.
Syntax:- Identifier IS [ NOT ] POSITIVE
NEGATIVE
ZERO
3. Class
Determines whether or not the value of an operand is numeric or alphabetic.
Syntax: - Identifier IS [NOT] NUMERIC
ALPHABETIC
4. Negated Simple
This condition places a NOT operator before a simple condition to reverse the condition.
Syntax:- Identifier IS NOT <Simple condition>
Eg: WS-A IS NOT > 5
5. Compound condition
This is used to connect two simple conditions by logical operators AND or OR to form a compound cond ition.
Syntax:- Condition-1 AND/OR Condition-2.
Eg: IF STATUS = ‘A’ OR ‘B’
6. Condition name condition
Wherever a condition can occur, such as in an IF statement or an EVALUATE or a PERFORM..UNTIL, a Condition
Name (Level 88) may be used.
Condition Names are defined in the DATA DIVISION using the special level number 88. They are always associated
with a data-item and are defined immediately after the definition of the data -item.
A Condition Name is a name given to a specified subset of the values which its associated data-item can hold. Like a
condition, a Condition Name evaluates to True or False. In the below example, rather than checking in the IF condition
as IF GENDER = 1 we can directly give IF MALE.
01 GENDER PIC 9.
88 MALE VALUE 1.
88 FEMALE VALUE 2.
IF MALE T HEN
DISPLAY ‘MALE’
ELSE
IF FEMALE
DISPLAY ‘FEMALE’
END-IF
END-IF
3.2. Cobol Sequence control statements
1. IF Statement
The IF command lets you conditionally perform a command. You can optionally specify an ELSE clause on the IF
command. If the test expression evaluates to false and an ELSE clause exists, the command associated with the ELSE
clause is performed. END-IF is the scope terminator which marks the logical end of IF stateme nt
Syntax:
IF {Condition}
Statements
ELSE
Statements
[END-IF].
Eg:-
IF (SAL <= 2500)
COMPUTE BONUS = 0.5 * SAL
ELSE
MOVE ZERO TO BONUS
END-IF.
If the SAL is less than or equal to 2500, then Bonus will be computed as per the logic else we will move zero to Bonus.
Nested IF
When more than one condition is to be tested, an IF within an IF is used. The flow of control in a NESTED IF is shown
in the Fig 7.
Syntax:
IF {Condition}
IF {Condition}
Statements
ELSE
Statements
END-IF
ELSE
Statements
[END-IF].
Fig.7 Nested IF loop
Problem Statement # 3
Write a program to calculate the bonus of the employees. The criteria for calculating the bonus is that If the salary of
the employee is less than 20000 INR then the bonus should be calculated as 5 % of the salary. If the salary is more
than 20000 INR then it should be 8 % of the salary. Display the salary after adding the bonus to it. The salary of the
employee needs to be accepted from the user.
Click here to download the solution
2. EVALUATE STATEMENT
Evaluate statement can be used instead of a set of IF statements to test several conditions. The syntax is given below.
END-EVALUATE is the scope terminator which will be the logical end of the Evaluate statement
EVALUATE Identifier-1
WHEN Identifier/Literal
Statement(s)
WHEN Identifier/Literal
Statement(s)
WHEN OTHER
Statement(s)]
END-EVALUATE
Example:
EVALUATE TRUE
WHEN STAT = ‘F’ OR ‘f’
MOVE ‘FULLTIME’ TO OUTPUT
WHEN STAT = ‘P’ OR ‘p’
MOVE ‘PART-TIME’TO OUTPUT
WHEN STAT = ‘R’ OR ‘r’
MOVE ‘RETIRED’ TO OUTPUT
WHEN OTHER
MOVE ‘ERROR' TO OUTPUT
END-EVALUATE.
It checks for the STAT field. If the first WHEN is satisfied then it will move 'FULLTIME' to OUTPUT and come out of the
loop. If the first condition is not satisfied, then it checks for the second WHEN and so on. If none of the conditions are
satisfied then it will go to WHEN OT HER and will move 'ERROR' to the output.
Problem statement #4
Write a program to categorize the students of a class into high, low and average performers. If the marks obtained in
the exam are more than 80, then the student should be categorized as a high performer. If the mark is between 60 &
80, then he is an average performer and if the mark is less than 60 then as a low performer. The mark for the subject
need to be accepted from the user and based on the marks, the performance of the student like high, average or low
need to be displayed on the screen.
Click here to download the solution
3. PERFORM statement
The perform statement is mainly used to execute a group of consecutive statements written in the program. Perform
statement acts very similar to GO TO statement except that after execution of the paragraph the control will be
transferred to the statement following to the next statement after PERFORM statement. END -PERFORM is the scope
terminator which will be the logical end of the PERFORM statement. Fig 5 depicts the control flow using GOTO &
PERFORM.
Fig 5. GOTO and PERFORM statement
There are different formats for PERFORM statements.
Format 1 - PERFORM paragraph-name
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ADD-PARA
STOP RUN.
ADD-PARA.
ADD NUM1, NUM2 GIVING RESULT.
DISPLAY "Result is = ", RESULT.
In the above example we perform ADD-PARA will take the control to the ADD-PARA and execute the logic inside that
paragraph. Once the logic is performed the control will return to the next statement after PERFORM which is STOP
RUN. STOP RUN will give the control back to the Operating System.
Format 2 - PERFORM para1 THRU para2
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ADD-PARA T HRU ADD-PARA-EXIT
STOP RUN.
ADD-PARA.
ADD NUM1, NUM2 GIVING RESULT.
DISPLAY "Result is = ", RESULT.
ADD-PARA-EXIT.
EXIT.
Here PARA1 and PARA2 are paragraph names. Execution of perform statement causes the control to transfer from the
first statement of the PARA1 to the last statement of PARA2 and then the control returns to the next executable
statement. Execution of perform statement will transfer the control to the ADD-PARA to the last statement of ADD-
PARA-EXIT. The last statement is EXIT statement which indicates the end of p aragraph execution and the control is
transferred back to the statement following the PERFORM THRU.
We will look into another program which will add 2 numbers and give the output in the RESULT field.
IDENT IFICATION DIVISION.
PROGRAM-ID. ADD1.
AUT HOR. XYZ.
DATA DIVISION.
WORKING-STORAGE SECT ION.
01 NUM1 PIC 9 VALUE 5.
01 NUM2 PIC 9 VALUE 4.
01 RESULT PIC 9 VALUE ZEROS.
PROCEDURE DIVISION.
ADD-PARA.
ADD NUM1, NUM2 GIVING RESULT.
DISPLAY "Result is = ", RESULT.
STOP RUN.
In the below program we are doing addition and subtraction operations. The addition and the subtraction operations are
mentioned in 2 paragraphs. When we mention PERFORM ADD-PARA it will go to the ADD-PARA and do the logic
mentioned in the paragraph. Here it is adding 2 numbers. Similarly when we perform SUB-PARA it will go to that
particular paragraph and perform the logic present in that paragraph. Finally we are displaying the RESULT and
RESULT1.
IDENT IFICATION DIVISION.
PROGRAM-ID. SUB1.
AUT HOR. XYZ.
DATA DIVISION.
WORKING-STORAGE SECT ION.
01 NUM1 PIC 9 VALUE 5.
01 NUM2 PIC 9 VALUE 4.
01 NUM3 PIC 9 VALUE 6.
01 NUM4 PIC 9 VALUE 2.
01 RESULT PIC 9 VALUE ZEROS.
01 RESULT1 PIC 9 VALUE ZEROS.
PROCEDURE DIVISION.
MAIN-PARA.
PERFORM ADD-PARA
PERFORM SUB-PARA
STOP RUN.
ADD-PARA.
ADD NUM1, NUM2 GIVING RESULT.
DISPLAY "Result is = ", RESULT.
SUB-PARA.
SUBTRACT NUM4 FROM NUM3 GIVING RESULT1.
DISPLAY "Result is = ", RESULT1.
Format 3 – PERFORM paragraph-name Varying
The syntax for the perform statement will be
PERFORM PARA-1 Varying WS-I FROM 1 BY 1 UNT IL WS-I >10
In the above perform, all statements in PARA-1 are executed till the condition specified in the UNT IL becomes true. For
the first iteration the value of WS-I will be 1 (as it is specified after FROM), for second iteration WS-I value increases by
1 and becomes 2 (as it is specified after BY keyword). For each iteration the statements in PARA-1 will be executed.
This iteration continues till the condition becomes true (that is WS-I >10).
Format 4 – PERFORM paragraph-name N TIMES
The syntax for the perform statement will be
PERFORM PARA-1 6 TIMES
In the above perform statement, logic in the paragraph PARA-1 will be executed 6 times. This format of the PERFORM
executes a block of code inside PARA-1 6 times before returning control to the statement following the PERFORM.
Problem statement #5
Write a program to find the factorial of a number (N). The number need to be provided as a user input and the factorial
of the number needs to be displayed on the screen
Problem state ment #6
Write a program to display the natural numbers from 1 till 5. The final output that needs to be displayed on the screen is
as shown below.
Problem statement #7
Write a program to print all the even numbers till 10. The final output that n eeds to be displayed on the screen is as
shown below.
10
Click here to download the solutions