Conditional Statements


A conditional statement derives the truth value (TRUE or FALSE) of a condition, and the further program execution flow is decided based on the truth value. These statements allow for decision-making in the program based on the evaluation of conditions. The conditional statements are -

  • IF Statement
  • EVALUATE Statement
  • Statements with conditional phrases

The conditional statements uses the condition to evaluate TRUE or FALSE. These condition is called as -

  • Conditional Expression

There are some statements that only works with conditional statements and those are -

  • CONTINUE
  • NEXT SENTENCE

IF Statement


IF statement validates a condition and decides the program flow depending on the condition validation. 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 a condition is TRUE.

IF condition-1
	statements-block-1 | NEXT SENTENCE
[END-IF].
statements-block-n.
  • condition-1 - Specifies condition.
  • statements-block-1, ... - These are the statements that gets executed when the associated condition is true.
  • NEXT SENTENCE - It transfers control to statement immediately following the period. We will discuss i
  • END-IF - It marks the end of the IF statement. If a period (.) can be coded at the last statement of the IF block, END-IF is not needed.

Example -

Scenario - Validating gender

IF WS-GENDER EQUAL 'M'
	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 
	statements-block-1
[ELSE]  
	statements-block-2
[END-IF].
statements-block-n.

If the condition-1 is true, the statements-block-1 gets executed first, and control transfers to statements-block-n. If the condition-1 is false, the statements-block-2 gets executed first, 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 coded 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 is used to validate multiple conditions at a time and controls the program flow based on the first condition match found. It is a shorter form for the nested IF...ELSE statements and simplifies the logic when multiple choices are available.

EVALUATE can be divided logically into the below types based on their usage in the program -

  • Simple EVALUATE
  • EVALUATE TRUE
  • EVALUATE with THRU
  • EVALUATE with multiple WHEN conditions
  • EVALUATE with MULTIPLE conditions

Simple EVALUATE -

EVALUATE has only one condition and validates a single variable against multiple values. The statements under the WHEN that match the variable 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 multiple conditions instead of checking a variable's value. 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 -

EVALUATE THRU is used to validate the variable with values range. The values range should be in ascending order.

Example - Validating student marks.

	 ...
     EVALUATE STD-MARKS
	     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 using multiple WHEN -

EVALUATE with multiple WHEN used to validate the variable with a set of values (different values). Multiple WHENs are grouped, and only one set of statements is coded for all WHEN phrases.

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 with Multiple Conditions -

EVALUATE with multiple 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.

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. The below COBOL statements become conditional when a condition is coded with them -

Conditional Expressions


The conditional expression is a condition or a part of a condition that evaluates to either TRUE or FALSE. These are used to choose the execution flow based on the truth value of the condition. Conditional expressions are used in decision-making statements, IF, IF...ELSE, EVALUATE, and in loop statements, PERFORM and SEARCH statements.

Those are -

CONTINUE


The 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


The NEXT SENTENCE statement transfers the control to the next COBOL statement immediately after the next period ('.'). It means skipping over certain statements and proceeding to the subsequent set of statements.

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). Because, COMPUTE statement have a period at the end and it is a sentence till the COMPUTE statement.