IF ... END IF
IF … END IF
Section titled “IF … END IF”IF is a very powerful control flow sentence that allows you to make decisions under specified conditions.
Syntax
Section titled “Syntax” IF expression [THEN] sentences [: END IF]
or
IF expression [THEN] sentences [ELSEIF expression [THEN] sentences] [ELSEIF expression [THEN] sentences] ... [ELSE sentences] END IF
Examples
Section titled “Examples”IF a < 5 THEN PRINT "A is less than five" ELSE PRINT "A is greater than five"
Sentences might be in multiple lines:
If a < 5 Then Print "A is less than five" a = a + 5Else Print "A is greater than five"End If
Since IF is a sentence, it can be nested; however, remember that every IF must be closed with END IF when the line is split after THEN (mutiline IF):
If a < 5 Then Print "A is less than five" If a > 2 Then Print "A is less than five but greater than 2" End IfElse If a < 7 Then Print "A is greater or equal to five, but lower than 7" Else Print "A is greater than five" End IfEnd If
Using ELSEIF
Section titled “Using ELSEIF”In the example above, you see that nesting an IF inside another one could be somewhat verbose and error prone. It’s better to use the ELSEIF construct. So the previous example could be rewritten as:
If a < 5 Then Print "A is less than five" If a > 2 Then Print "A is less than five but greater than 2" End IfElseIf a < 7 Then Print "A is greater or equal to five, but lower than 7"Else Print "A is greater than five"End If
Remarks
Section titled “Remarks”- This sentence is extended allowing now multiline IFs and also compatible with the Sinclair BASIC version.
- Starting from version 1.8 onwards the trailing END IF is not mandatory for single-line IFs, for compatibility with Sinclair BASIC
- The THEN keyword can be omitted, but keep in mind this might reduce code legibility.