IF/END IF block

Purpose

Create IF/THEN/ELSE constructs with multiple lines and/or conditions.

Syntax

IF integer_expression THEN

  {statements}

[ELSEIF integer_expression THEN

  {statements}]

[ELSE

  {statements}]

END IF

Remarks

In executing IF blocks, the truth of the integer_expression in the initial IF statement is checked first.  If it evaluates to FALSE (zero), each of the following ELSEIF statements is examined in order.  There can be as many ELSEIF statements as desired.  As soon as one is found to be TRUE (non-zero), PowerBASIC executes the statement(s) following the associated THEN and before the next ELSEIF or ELSE.

Execution then jumps to the statement just after the terminating END IF without making any further tests.  If none of the test expressions evaluates to TRUE, the statement(s) in the ELSE clause (which is optional) are executed.

Note that there must be nothing following the THEN keyword in an IF block; that's how the compiler distinguishes an IF block from a conventional IF statement.  There must also be nothing on the same line as the ELSE (except for remarks).

IF blocks can be nested; that is, any of the statements after any of the THENs may contain IF blocks.  Although the compiler doesn't care, the clarity of source code is improved by indenting the statements controlled by each test a couple of spaces, as shown in the example.

IF blocks must be terminated with a matching END IF statement.  Note that the END IF statement requires a space and the ELSEIF statement does not.

See the IF statement for notes on PowerBASIC's Short-circuit evaluation and its possible side effects.

See also

CHOOSE, CHOOSE&, CHOOSE$EXIT, IF, IIF, IIF&, IIF$, MAX, MAX&, MAX$, MIN, MIN&, MIN$, SELECT, Short-circuit evaluation, SWITCH, SWITCH&, SWITCH$, SELECT

Example

X = (RND * 500) + 1

IF X = 1 THEN

  x$ = "The number is 1"

ELSEIF X = 2 THEN

  x$ = "The number is 2"

ELSE

  IF X < 50 THEN

    x$ = "The number is less than 50"

  ELSEIF X < 100 THEN

    x$ = "Greater than 49 and less than 100"

  ELSE

    x$ = "The number is 100 or greater"

  END IF

END IF