Conditional Statements

Conditional statements are used to change the execution flow depending on certain conditions specified by the programmer. Conditional statements will always evaluate to true or false. Conditions are used in IF, Evaluate, and Perform statements. The different types of conditions are as follows −

  • IF Condition Statement
  • Relation Condition
  • Sign Condition
  • Class Condition
  • Condition-Name Condition
  • Negated Condition
  • Combined Condition

IF Condition Statement

IF statement checks for conditions. If a condition is true, the IF block is executed; and if the condition is false, the ELSE block is executed.

END-IF is used to end the IF block. To end the IF block, a period can be used instead of END-IF. But it is always preferable to use END-IF for multiple IF blocks.

Nested-IF − IF blocks appearing inside another IF block. There is no limit to the depth of nested IF statements.

Syntax

Following is the syntax of IF condition statements −

IF [condition] THEN
   [COBOL statements]
ELSE
   [COBOL statements]
END-IF.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9).01 WS-NUM2 PIC9(9).01 WS-NUM3 PIC9(5).01 WS-NUM4 PIC9(6).PROCEDUREDIVISION.
   A000-FIRST-PARA.MOVE25TO WS-NUM1 WS-NUM3.MOVE15TO WS-NUM2 WS-NUM4.IF WS-NUM1 > WS-NUM2 THENDISPLAY'IN LOOP 1 - IF BLOCK'IF WS-NUM3 = WS-NUM4 THENDISPLAY'IN LOOP 2 - IF BLOCK'ELSEDISPLAY'IN LOOP 2 - ELSE BLOCK'END-IFELSEDISPLAY'IN LOOP 1 - ELSE BLOCK'END-IF.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

IN LOOP 1 - IF BLOCK
IN LOOP 2 - ELSE BLOCK

Relation Condition

Relation condition compares two operands, either of which can be an identifier, literal, or arithmetic expression. Algebraic comparison of numeric fields is done regardless of size and usage clause.

For non-numeric operands

If two non-numeric operands of equal size are compared, then the characters are compared from left with the corresponding positions till the end is reached. The operand containing greater number of characters is declared greater.

If two non-numeric operands of unequal size are compared, then the shorter data item is appended with spaces at the end till the size of the operands becomes equal and then compared according to the rules mentioned in the previous point.

Syntax

Given below is the syntax of Relation condition statements −

[Data Name/Arithmetic Operation]

   [IS] [NOT] 

[Equal to (=),Greater than (>), Less than (<), 
Greater than or Equal (>=), Less than or equal (<=) ]

[Data Name/Arithmetic Operation] 

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(9).01 WS-NUM2 PIC9(9).PROCEDUREDIVISION.
   A000-FIRST-PARA.MOVE25TO WS-NUM1.MOVE15TO WS-NUM2.IF WS-NUM1 ISGREATERTHANOREQUALTO WS-NUM2 THENDISPLAY'WS-NUM1 IS GREATER THAN WS-NUM2'ELSEDISPLAY'WS-NUM1 IS LESS THAN WS-NUM2'END-IF.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program it produces the following result −

WS-NUM1 IS GREATER THAN WS-NUM2

Sign Condition

Sign condition is used to check the sign of a numeric operand. It determines whether a given numeric value is greater than, less than, or equal to ZERO.

Syntax

Following is the syntax of Sign condition statements −

[Data Name/Arithmetic Operation] 

   [IS] [NOT] 

[Positive, Negative or Zero]

[Data Name/Arithmetic Operation]

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(9)VALUE-1234.01 WS-NUM2 PICS9(9)VALUE123456.PROCEDUREDIVISION.
   A000-FIRST-PARA.IF WS-NUM1 ISPOSITIVETHENDISPLAY'WS-NUM1 IS POSITIVE'.IF WS-NUM1 ISNEGATIVETHENDISPLAY'WS-NUM1 IS NEGATIVE'.IF WS-NUM1 ISZEROTHENDISPLAY'WS-NUM1 IS ZERO'.IF WS-NUM2 ISPOSITIVETHENDISPLAY'WS-NUM2 IS POSITIVE'.STOPRUN.

JCL to execute the above COBOL program −

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program it produces the following result −

WS-NUM1 IS GREATER THAN WS-NUM2

Sign Condition

Sign condition is used to check the sign of a numeric operand. It determines whether a given numeric value is greater than, less than, or equal to ZERO.

Syntax

Following is the syntax of Sign condition statements −

[Data Name/Arithmetic Operation] 

   [IS] [NOT] 

[Positive, Negative or Zero]

[Data Name/Arithmetic Operation]

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICS9(9)VALUE-1234.01 WS-NUM2 PICS9(9)VALUE123456.PROCEDUREDIVISION.
   A000-FIRST-PARA.IF WS-NUM1 ISPOSITIVETHENDISPLAY'WS-NUM1 IS POSITIVE'.IF WS-NUM1 ISNEGATIVETHENDISPLAY'WS-NUM1 IS NEGATIVE'.IF WS-NUM1 ISZEROTHENDISPLAY'WS-NUM1 IS ZERO'.IF WS-NUM2 ISPOSITIVETHENDISPLAY'WS-NUM2 IS POSITIVE'.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program it produces the following result −

WS-NUM1 IS NEGATIVE
WS-NUM2 IS POSITIVE

Class Condition

Class condition is used to check if an operand contains only alphabets or numeric data. Spaces are considered in ALPHABETIC, ALPHABETIC-LOWER, and ALPHABETIC-UPPER.

Syntax

Following is the syntax of Class condition statements −

[Data Name/Arithmetic Operation>]

   [IS] [NOT] 

[NUMERIC, ALPHABETIC, ALPHABETIC-LOWER, ALPHABETIC-UPPER]

[Data Name/Arithmetic Operation]

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PICX(9)VALUE'ABCD '.01 WS-NUM2 PIC9(9)VALUE123456789.PROCEDUREDIVISION.
   A000-FIRST-PARA.IF WS-NUM1 ISALPHABETICTHENDISPLAY'WS-NUM1 IS ALPHABETIC'.IF WS-NUM1 ISNUMERICTHENDISPLAY'WS-NUM1 IS NUMERIC'.IF WS-NUM2 ISNUMERICTHENDISPLAY'WS-NUM2 IS NUMERIC'.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

WS-NUM1 IS ALPHABETIC
WS-NUM2 IS NUMERIC

Condition-name Condition

A condition-name is a user-defined name. It contains a set of values specified by the user. It behaves like Boolean variables. They are defined with level number 88. It will not have a PIC clause.

Syntax

Following is the syntax of user-defined condition statements −

88 [Condition-Name] VALUE [IS, ARE] [LITERAL] [THRU LITERAL].

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM PIC9(3).88 PASS VALUESARE041THRU100.88 FAIL VALUESARE000THRU40.PROCEDUREDIVISION.
   A000-FIRST-PARA.MOVE65TO WS-NUM.IF PASS 
  DISPLAY'Passed with ' WS-NUM ' marks'.IF FAIL 
  DISPLAY'FAILED with ' WS-NUM 'marks'.STOPRUN.</pre>

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

Passed with 065 marks

Negated Condition

Negated condition is given by using the NOT keyword. If a condition is true and we have given NOT in front of it, then its final value will be false.

Syntax

Following is the syntax of Negated condition statements −

IF NOT [CONDITION] 
   COBOL Statements
END-IF.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(2)VALUE20.01 WS-NUM2 PIC9(9)VALUE25.PROCEDUREDIVISION.
   A000-FIRST-PARA.IFNOT WS-NUM1 ISLESSTHAN WS-NUM2 THENDISPLAY'IF-BLOCK'ELSEDISPLAY'ELSE-BLOCK'END-IF.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

ELSE-BLOCK

Combined Condition

A combined condition contains two or more conditions connected using logical operators AND or OR.

Syntax

Following is the syntax of combined condition statements −

IF [CONDITION] AND [CONDITION]
   COBOL Statements
END-IF.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-NUM1 PIC9(2)VALUE20.01 WS-NUM2 PIC9(2)VALUE25.01 WS-NUM3 PIC9(2)VALUE20.PROCEDUREDIVISION.
   A000-FIRST-PARA.IF WS-NUM1 ISLESSTHAN WS-NUM2 AND WS-NUM1=WS-NUM3 THENDISPLAY'Both condition OK'ELSEDISPLAY'Error'END-IF.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

Both condition OK

Evaluate Verb

Evaluate verb is a replacement of series of IF-ELSE statement. It can be used to evaluate more than one condition. It is similar to SWITCH statement in C programs.

Example

IDENTIFICATIONDIVISION.PROGRAM-ID. HELLO.DATADIVISION.WORKING-STORAGESECTION.01 WS-A PIC9VALUE0.PROCEDUREDIVISION.MOVE3TO WS-A.EVALUATETRUEWHEN WS-A >2DISPLAY'WS-A GREATER THAN 2'WHEN WS-A <0DISPLAY'WS-A LESS THAN 0'WHENOTHERDISPLAY'INVALID VALUE OF WS-A'END-EVALUATE.STOPRUN.

JCL to execute the above COBOL program −

//SAMPLE JOB(TESTJCL,XXXXXX),CLASS= A,MSGCLASS = C
//STEP1 EXEC PGM = HELLO

When you compile and execute the above program, it produces the following result −

WS-A GREATER THAN 2

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *