Forms of the IF Statement

Assume the IF statement you are using is of the following form:

IF BEXP THEN S1

If the value of the Boolean expression BEXP is TRUE, the statement S1 is executed. If BEXP is FALSE, then S1 is not executed. In either case, execution continues with the statement following the IF statement.

Assume the IF statement you are using is of the following form:

IF BEXP THEN S1 ELSE S2

If the value of BEXP is TRUE, the statement S1 is executed and the statement S2 is ignored. If the value of BEXP is FALSE, then the statement S2 is executed, and S1 is ignored. In either case, execution continues with the statement following the IF statement.

Note that both block statements and compound statements can be substituted for <statement> in the IF statement.

IF statements can be nested; that is, the statements following the reserved words THEN or ELSE (or both) can also be IF statements.

When IF statements are nested, the correct correspondence between the reserved words THEN and ELSE must be maintained. The compiler matches the innermost THEN to the first ELSE that follows it and that yields a syntactically correct IF statement. Consider the following IF statement:

IF BEXP1 THEN IF BEXP2 THEN S2 ELSE S1

The ELSE is paired with the innermost THEN, which is the THEN following BEXP2, as illustrated in the following:

IF BEXP1 THEN
   IF BEXP2 THEN
      S2
   ELSE
      S1

If the program pairs the ELSE with the THEN following BEXP1, the inner IF statement must be made a compound statement by using BEGIN and END as follows:

IF BEXP1 THEN
   BEGIN
   IF BEXP2 THEN
      S2
   END
ELSE
   S1

A GO TO statement can lead to a labeled statement within an IF statement. The subsequent action is equivalent to the action that would result if the IF statement were entered at the beginning and evaluation of the Boolean expression caused execution of the labeled statement.