Conditional Statements
A conditional statement derives a condition's truth value (TRUE or FALSE), and decides the further program execution flow. These statements allow decision-making in the program based on the evaluation of conditions. The conditional statements are -
- IF Statement
- EVALUATE Statement
- Statements with conditional phrases
Some statements only work with conditional statements, and those are -
- CONTINUE
- NEXT SENTENCE
IF Statement
IF statement validates a condition to get the truth value and executes the statements under IF when the condition is TRUE. It comes under selective programming. IF statements are of three types based on their usage in the program -
- Simple IF
- IF...ELSE
- Nested IF
Simple IF -
A simple IF statement is used to execute the set of statements only if the condition is TRUE.
IF condition-1 [THEN]
statements-block-1
[END-IF].
statements-block-n.
- condition-1 - Specifies condition.
- statements-block-1, ... - statements block that gets executed when the associated condition is true.
- END-IF - Specifies the end of the IF statement. If a period (.) is coded at the last statement of the IF block, END-IF is not needed.
Example -
Scenario - Validating gender
IF WS-GENDER EQUAL 'M' [THEN]
DISPLAY "Person is Male"
END-IF.
IF...ELSE -
IF...ELSE condition is used to validate a condition and handles both (either TRUE or FALSE) outcomes of the condition.
IF condition-1 [THEN]
statements-block-1
[ELSE]
statements-block-2
[END-IF].
statements-block-n.
If the condition-1 is true, the statements-block-1 gets executed, and control transfers to statements-block-n. If the condition-1 is false, the statements-block-2 gets executed, and control transfers to statements-block-n.
Example -
Scenario - Validating gender
IF WS-GENDER EQUAL 'M'
DISPLAY "Person is Male"
ELSE
DISPLAY "person is Female"
END-IF.
Nested IF -
IF statement within another IF statement is called as NESTED IF statement. In Nested IF, all IF statements should have their matching END-IF.
IF condition-1
IF condition-2
statements-block-1
[ELSE
statements-block-2
END-IF]
[ELSE
IF condition-3
statements-block-3
[ELSE
statements-block-4
END-IF]
END-IF].
statements-block-n.
Example -
Scenario - Nested IF..ELSE statement for validating marks percent
IF WS-MARKS-PERCENT > 60
DISPLAY 'GOT FIRST CLASS'
ELSE
IF WS-MARKS-PERCENT > 50
DISPLAY 'GOT SECOND CLASS'
ELSE
IF WS-MARKS-PERCENT > 35
DISPLAY 'GOT THIRD CLASS'
ELSE
DISPLAY 'FAILED'
END-IF
END-IF
END-IF.
EVALUATE Statement
EVALUATE validates multiple conditions at a time and controls the program flow. It is a shorter form for the nested IF...ELSE and simplifies the logic when multiple choices are available.
EVALUATE logically divided into the below types based on their usage in the program -
- Simple EVALUATE
- EVALUATE TRUE
- EVALUATE with THRU
- EVALUATE with grouping multiple WHEN conditions
- EVALUATE...WHEN with combined conditions
Simple EVALUATE -
EVALUATE validates a single variable against multiple values. The WHEN statement has valid values, and the statements block under the WHEN that match the value get executed.
Example - Displaying weekday using day number in week.
...
EVALUATE WEEK-DAY
WHEN 01
DISPLAY "TODAY IS SUNDAY"
...
WHEN 07
DISPLAY "TODAY IS SATURDAY"
WHEN OTHER
DISPLAY "INVALID INPUT"
END-EVALUATE.
EVALUATE TRUE -
Evaluating different conditions instead of validating a single condition. EVALUATE has the boolean value (TRUE or FALSE), and WHENs have the logical expressions.
Example - Validating weekday using day number in week.
...
EVALUATE TRUE
WHEN WEEK-DAY = 01
DISPLAY "TODAY IS SUNDAY"
...
WHEN WEEK-DAY = 07
DISPLAY "TODAY IS SATURDAY"
WHEN OTHER
DISPLAY "Invalid Input"
END-EVALUATE.
EVALUATE with THRU -
Evaluating the variable value with range of values. The values range should be in ascending order.
Example - Validating student marks percentage.
...
EVALUATE WS-MARKS-PERCENT
WHEN 60 THRU 100
DISPLAY 'Student got FIRST CLASS'
WHEN 50 THRU 59
DISPLAY 'Student got SECOND CLASS'
WHEN 35 THRU 49
DISPLAY 'Student got THIRD CLASS'
WHEN OTHER
DISPLAY 'Student Failed'
END-EVALUATE.
EVALUATE with grouping multiple WHEN conditions -
EVALUATE with multiple WHEN is used to execute the same statement block for multiple WHEN conditions. Various WHENs are grouped in this case, and only one statement block will be coded under them.
Example - Validating student grades.
...
EVALUATE STD-GRADE
WHEN "A"
WHEN "B"
WHEN "C"
WHEN "D"
DISPLAY 'Student got FIRST CLASS'
WHEN "E"
DISPLAY 'Student got SECOND CLASS'
WHEN "F"
WHEN "G"
DISPLAY 'Student got THIRD CLASS'
WHEN OTHER
DISPLAY 'Student Failed'
END-EVALUATE.
EVALUATE...WHEN with combined conditions -
EVALUATE...WHEN with combined conditions used to validate the set of conditions combined with ALSO. The number of conditions in the EVALUATE should match the number of conditions with the WHEN phrase.
Example - Validating age and gender.
...
EVALUATE TRUE ALSO TRUE
WHEN WS-AGE > 18 ALSO WS-GENDER = 'M'
DISPLAY 'HE IS MAJOR'
WHEN WS-AGE <= 18 ALSO WS-GENDER = 'M'
DISPLAY 'BOY IS MINOR'
WHEN WS-AGE > 18 ALSO WS-GENDER = 'F'
DISPLAY 'SHE IS MAJOR'
WHEN WS-AGE <= 18 ALSO WS-GENDER = 'M'
DISPLAY 'GIRL IS MINOR'
WHEN OTHER
DISPLAY 'Invalid Input'
END-EVALUATE.
Conditional Expressions
Conditional expression is a condition or a part of a condition that evaluates either TRUE or FALSE. These expressions are used to choose the execution flow based on the truth value of the condition. These expressions are used in decision-making statements (IF, IF...ELSE, EVALUATE), and in loop statements (PERFORM and SEARCH).
Those are -
- CLASS Condition - CLASS condition tests whether the data in a variable belongs to a specific category of characters such as ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER, NUMERIC, etc.
- Conditional Names - is a meaningful name for a value or a range of values of a variable. IF and EVALUATE statements validate whether the condition variable's value is equal to any values associated with the condition name.
- Relation Condition - compares two operands using relational operators (=, >, <, <=, >=, <>) and returns a boolean value of TRUE or FALSE.
- SIGN Condition - tests the sign (positive, negative, or zero) of the data in numeric variables.
- Combined (AND|OR) Condition - is a logical group with two or more conditions and is used to test multiple conditions to produce a single decision.
- Negated Condition - A condition is negated with the use of the logical operator NOT.
CONTINUE
CONTINUE statement transfers the control to the immediate COBOL statement that comes next in the program flow. It is a no-operation, and it is a do-nothing statement.
CONTINUE
Example - Salary is greater than 5000, do nothing.
Input - WS-SALARY = 6000.
...
IF WS-SALARY GREATER THAN 5000
CONTINUE
ELSE
COMPUTE WS-SALARY = WS-SALARY + 2000
END-IF.
* Displaying Salary
DISPLAY "SALARY: " WS-SALARY.
In the above example, the input salary is 6000 which is greater than 5000. So, the CONTINUE gets executed and control transfers to the next statement in the flow (DISPLAY statement).
NEXT SENTENCE
NEXT SENTENCE transfers the control to the following COBOL statement immediately after the explicit scope terminator (period - '.') in the flow.
NEXT SENTENCE
Example - Salary is greater than 5000, do nothing.
Input - WS-SALARY = 7000.
...
IF WS-SALARY GREATER THAN 5000
NEXT SENTENCE
END-IF
COMPUTE WS-SALARY = WS-SALARY + 2000.
* Displaying Salary
DISPLAY "SALARY: " WS-SALARY.
In the above example, the input salary is 7000, which is greater than 5000. So, the NEXT SENTENCE gets executed, and control transfers to the next sentence in the flow (DISPLAY statement). This is because the COMPUTE has a period at the end, and it is a sentence until the COMPUTE.
Statements with conditional phrases -
If any statement uses the SIZE ERROR, OVERFLOW, AT END, INVALID KEY, AND EXCEPTION phases, those are also called conditional statements. Below COBOL statements become conditional when a condition is coded with them -
Arithmetic Statements -
- ADD...[NOT] ON SIZE ERROR
- SUBTRACT...[NOT] ON SIZE ERROR
- MULTIPLY...[NOT] ON SIZE ERROR
- DIVIDE...[NOT] ON SIZE ERROR
- COMPUTE...[NOT] ON SIZE ERROR
String Handling -
Table Handling -
Input Output Handling -
- READ...[NOT] AT END
- READ...[NOT] INVALID KEY
- START...[NOT] INVALID KEY
- WRITE...[NOT] INVALID KEY
- REWRITE...[NOT] INVALID KEY
- DELETE...[NOT] INVALID KEY
Ordering -
Program or method linkage -