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 IF

Also 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

  • THEN is required on the one-line form: it marks where the condition ends. Without anything after THEN, the line opens a block and needs ENDIF or END IF.
  • On the one-line form, every statement joined by : after THEN belongs to the THEN branch, and every one after ELSE belongs to the ELSE branch.
  • ELSEIF and ELSE IF are the same word, with or without THEN. ELSE comes once and last: an ELSEIF after it, or a second ELSE, 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. Write IF x = 1 AND y = 2 THEN … or a block.
  • On the one-line form, the condition must not contain a string with THEN in 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")   ' pass
In the manual : Control flow