COBOL IF Statement

COBOL Selective Program Construction COBOL IF Conditional Expression

Advertisements


IF statement decide the program flow based on the condition validation.

IF statement comes under condition validation programming.

IF statement mainly three types based on its usage in the program.


  1. Simple IF
  2. IF ELSE
  3. Nested IF

Simple IF:


Simple IF mainly used to execute the condition specific code.

In IF, the set of statements executed if the specified condition is satisfied.

IF condition not satisfied, the control will transfer to the next statements after IF statement terminated.


Syntax:

IF Condition  
	{Statement Block}
[END-IF].

Scope terminator END-IF is optional in the program.

The period can be specifies at the last statement of block provided in IF statement.

If period not specified then scope terminator END-IF is required.

IF the condition satisfies, then the statement block under the IF will be executed. If the condition fails, the block will be skipped.


IF ELSE:


IF ELSE statement used when certain set of statements flow needs to be decided by two conditions.

IF ELSE mainly used to execute the condition specific code.

In IF ELSE, the set of statements executed if the specified condition is satisfied.

IF condition not satisfied, the other set of statements will be executed and those will set will be under ELSE part.

Based on the condition result in the IF condition, the program flow will be decided.


Syntax:

IF Condition-1
	{Statement-Block-1/NEXT SENTENCE}
[ELSE]  
	{Statement-Block-2/NEXT SENTENCE}
[END-IF]

IF statement execution will be like below

If the condition of IF statement is true then the statements block will be executed. If the condition is false then the statements block under ELSE statement will be executed. Here the END-IF is the scope terminator for IF statement which will describes the end of IF statement.

In IF ELSE, ELSE clause is optional.

Scope terminator END-IF is optional in the program.

The period can be specifies at the last statement of block provided in ELSE part.

If period not specified then scope terminator END-IF is required to determine the end of IF condition.


NESTED IF:


COBOL allows the NESTED IF statement. IF statement with in the IF statement called as NESTED IF statement.

NESTED IF contains a chain of conditions which are more than two.

NESTED IF mainly used to execute the condition specific code.

In NESTED IF, the set of statements executed if the specified condition is satisfied.


Syntax:

IF Condition-1 THEN
	IF Condition-2 THEN
		Statements-block-1
	[ELSE
		Statements-block-2
	END-IF]
[ELSE
	IF Condition-3 THEN
		Statements-block-3
	[ELSE
		Statements-block-4
	END-IF]
END-IF.]

If condition-1 satisfied, it will check for condition-2.

If condition-2 also satisfied, then Statements-block-1 will be executed and control transfers to the next executable statement after IF statement.

If condition-2 is failed, then statements-block-2 will be executed and control transfers to the next executable statement after IF statement.

If condition-1 failed, then the control will go to the else part and validate condition-3.

If condition-3 satisfied, then Statements-block-3 will be executed and control transfers to the next executable statement after IF statement.

If condition-3 is failed, then statements-block-4 will be executed and control transfers to the next executable statement after IF statement.


Practical Example:


Code:



Jcl:



Output:



COBOL Selective Program Construction COBOL IF Conditional Expression

Advertisements