Syntax
Runs a block of statements only when a condition is true, with optional ELSEIF and ELSE branches. The first true branch runs and the others are skipped.
IF <condition> THEN <statement> [: <statement>…] [ELSE <statement> [: <statement>…]]
IF <condition> [THEN]
<statements>
[ELSEIF <condition> [THEN]
<statements>]…
[ELSE IF <condition> [THEN]
<statements>]…
[ELSE
<statements>]
ENDIF
END IFAlso written : THEN, ELSEIF, ELSE, ENDIF, END IF, ELSE IF
Parts
| <condition> | Any expression. 0, an empty string, FALSE and NULL are false; anything else is true. In a condition, = compares and never assigns. |
|---|---|
| <statement> | One statement of the one-line form. Several can follow THEN or ELSE, joined by :. |
| <statements> | The lines of a branch of the block form. |
Good to know
THENis required on the one-line form: it marks where the condition ends. Without anything afterTHEN, the line opens a block and needsENDIForEND IF.- On the one-line form, every statement joined by
:afterTHENbelongs to the THEN branch, and every one afterELSEbelongs to the ELSE branch. ELSEIFandELSE IFare the same word, with or withoutTHEN.ELSEcomes once and last: anELSEIFafter it, or a secondELSE, is a compile error.- A one-line IF cannot hold another one-line IF:
IF x = 1 THEN IF y = 2 THEN …compiles, but the program does not start. WriteIF x = 1 AND y = 2 THEN …or a block. - On the one-line form, the condition must not contain a string with
THENin it:IF a$ = "X THEN Y" THEN …compiles, but the program does not start. Store the text in a variable first, or use the block form. - A block IF does not create a variable scope: a variable first set inside it is still readable after
ENDIF.
Example
score = 75IF score >= 90 Console("A")ELSEIF score >= 70 THEN Console("B") ' BELSE Console("C")ENDIFIF score > 50 THEN Console("pass") ELSE Console("fail") ' passIn the manual : Control flow