IF

Purpose

Conditionally executes code based on a test expression.

Syntax

IF test-expression THEN clause[;ELSEclause];

Parameters

test-expression
Any expression that results in a scalar bit string value of length ≥1. The test-expression is considered true if any bit in the string is one; the expression is considered false only when all bits are zero.
clause
Either a BEGIN block, DO-group, or statement other than END, PROCEDURE, DECLARE, or FORMAT.

Description

The IF statement evaluates the test-expression. If the expression is true, the THEN clause is executed; otherwise, the ELSE clause, if present, is executed.

When an IF statement is used as a THEN or ELSE clause of another IF statement, any ELSE is always matched with the nearest unmatched preceding THEN.

test-expressions in IF statements may produce bit(n) values, where n>1. In this case, the test expression will be considered true if any of the bits in the string are 1 and false only when all bits are 0.

Note: For information about evaluating short-circuited IF statements, see Short-circuit Evaluation.

Example

IF A>B
   THEN IF C>D
      THEN X = 5; 
      ELSE X = 10;

In this example, the ELSE clause is associated with IF C > D THEN X = 5 ;

To associate an ELSE with the first THEN, you may write an ELSE or an empty ELSE with the second THEN, as shown in the following examples.

IF A>B;
   IF C>D THEN X=5; 
   ELSE;
ELSE X = 10;

or

IF A>B THEN DO;
   IF C>D THEN X=5; 
   END;
ELSE X = 10;

Restrictions

None.