IF Statement

The IF statement tests an expression and performs a specified action if the result of the expression is true. Its general form is:

IF expression
    THEN statement; 
    [ELSE statement];

where expression is any valid expression that yields a scalar bit-string value, and statement is any unlabeled statement (except DECLARE, END, ENTRY, FORMAT, or PROCEDURE) or an unlabeled DO-group or BEGIN block. For example:

IF A>B
    THEN READ FILE (F) INTO (X);

In this example, the IF statement contains the READ statement. If the expression A>B is true, the READ statement executes.

The IF statement needs no semicolon of its own because each statement contained within it has its own semicolon. IF statements can be nested, as shown in the following example.

IF A>B
    THEN IF D<C
        THEN READ FILE(F) INTO(X); 
        ELSE STOP;

In this example, the second IF statement has both a THEN clause and an ELSE clause. An ELSE clause always corresponds to the THEN clause that immediately precedes it. The first IF statement has only a THEN clause. If you want to STOP when A ^> B, you may provide a null ELSE clause to the innermost IF, forcing the ELSE STOP to be associated with the first IF. It is preferable to use structured IF, THEN, and DO statements to accomplish this, as shown in the following example.

IF A>B THEN DO;
    IF D<C THEN READ FILE(F) INTO (X); 
    END;
ELSE STOP;