Summary -

In this topic, we described about the below sections -

The IF statement evaluates a condition and decides the program flow depending on the evaluation. IF statement decides the program flow based on the condition validation. IF statement comes under selective programming construction.

Syntax -

IF Syntax

condition-1 -

Can be any simple or complex condition.

statement-1, statement-2 -

can be an imperative statement or conditional statement or an imperative statement followed by a conditional statement.

NEXT SENTENCE -

The NEXT SENTENCE phrase transfers control to statement immediately following the next separator period (even though END-IF specified).

END-IF phrase -

The END-IF used to end the scope of the IF statement. END-IF permits a conditional IF statement to be nested in another conditional statement. The IF statement scope can be terminated by any of the following -

  • An END-IF phrase at the same level of nesting.
  • A separator periods.
  • If nested, by an ELSE phrase associated with an IF statement at a higher level of nesting.

The END-IF or ELSE phrase not specified, system considers the statement that has the separator period as a end of IF statement.

IF Types -

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

  • Simple IF
  • IF ELSE
  • Nested IF

Simple IF: -

Simple IF statement used to execute the set of statements only if condition-1 is satisfied. If the condition-1 is not satisfied, the control transfers to the next statements after IF statement.

Syntax -

IF condition-1  
	{statement-block-1}
[END-IF].
statements-block-n.

If the condition-1 satisfies, then the statement-block-1 gets executed. If the condition-1 fails, the statement-block-1 skipped and the control transfers to the statements-block-n.

IF ELSE: -

IF statement used to execute the set of statements if condition-1 is satisfied and another set of statements if condition-1 is not satisfied. In this scenario, at least one set of statements gets executed in any case.

Syntax -

IF condition-1
	{statement-block-1/NEXT SENTENCE}
[ELSE]  
	{statement-block-2/NEXT SENTENCE}
[END-IF].
statements-block-n.

If the condition-1 is true, then the statement-block-1 gets executed first and then control transfers to statements-block-n.

If the condition-1 is false, then the statement-block-2 gets executed first and then control transfers to statements-block-n. In IF ELSE statement, ELSE clause is optional.

If ELSE phrase is specified, the separator period should not be coded before ELSE phrase. If separator period is coded before ELSE part, the compilation error occurs by specifying "No matching IF statement found for ELSE".

NESTED IF: -

IF statement coded within another IF statement called as NESTED IF statement. Nested IF statements are considered to be matched IF, ELSE and END-IF combinations proceeding from left to right.

Any IF statement that matches with ELSE at the same level has been implicitly or explicitly terminated. Any END-IF encountered is matched with the nearest preceding IF that has not been implicitly or explicitly terminated.

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].
statements-block-n.

If condition-1 satisfied, the condition-2 is evaluated. If condition-2 also satisfied, then statements-block-1 gets executed first and then control transfers to statements-block-n.

If condition-2 is not satisfied, then statements-block-2 gets executed first and then control transfers to statements-block-n. If condition-1 is not satisfied, then the control transfers to ELSE part and validate condition-3.

If condition-3 satisfied, then statements-block-3 gets executed first and then control transfers to statements-block-n. If condition-3 is not satisfied, then statements-block-4 gets executed first and then control transfers to statements-block-n.


Practical Example -

Code-

If Condition Code

Jcl-

If Condition Jcl

Output-

If Condition Output