IF statement
The IF statement evaluates an expression and controls the flow of execution according to the result of that evaluation. The IF statement thus provides a conditional branch.
- expression
- The expression must have the attributes BIT(1) NONVARYING unless
RULES(LAXIF) is used.
When expressions involve the use of the & or | operators, they are evaluated as described in Combinations of operations.
- unit
- Either a valid single statement, a group, or a begin-block. All
single statements are considered valid and executable except DECLARE,
DEFAULT, END, ENTRY FORMAT, PROCEDURE, or a %statement. If a nonexecutable
statement is used, the result can be unpredictable. Each unit can
contain statements that specify a transfer of control (for example,
GO TO). Hence, the normal sequence of the IF statement can be overridden.
Each unit can be labeled and can have condition prefixes.
IF is a compound statement. The semicolon terminating the last unit also terminates the IF statement.
If any bit in the string expression has the value '1'B, unit1 is executed and unit2, if present, is ignored. If all bits are '0'B, or the string is null, unit1 is ignored and unit2, if present, is executed.
IF statements can be nested. That is, either unit can itself be an IF statement, or both can be. Because each ELSE is always associated with the innermost unmatched IF in the same block or do-group, an ELSE with a null statement might be required to specify a desired sequence of control. Consider the following example:
if A = B then
.
.
.
else
if A = C then
.
.
.
else
.
.
.
If B and C are constants, this
example is equivalent to the following statement and would be better
coded as follows:
select( A );
when ( B )
.
.
.
when ( C )
.
.
.
end;
