If
Statement If
There are two forms of IF statement:
IF...THEN and the IF...THEN...ELSE as a single line statement.
IF...THEN...ENDIF and the IF...THEN...ELSE ...ENDIF as a multi line (or block) statement.
Single Line form
IF expression THEN statement1 ELSE statement2
Where expression returns a boolean value. If expression is true (non zero result), then statement1 is executed; otherwise statement2, following the else clause is executed.
Multi-Line (Block) form
IF expression THEN
statement
statement
statement
ELSE
statement
statement
ENDIF
For example, as a single line statement
IF J <> 0 then Result = I/J else Result = 0 ' Avoid a divide by zero error.
Written as a block statement (therefore requiring the ENDIF keyword):
IF J <> 0 THEN ' Nothing must appear after THEN except a comment
Result = I / J
ELSE ' Nothing must appear after ELSE except a comment
Result = 0
ENDIF
Most programming books and modern languages prefer the Block (Multi-line) form for the IF statement. It is more structured and easier to follow.
Since there is no statement separator provided in this scripting language, only a single THEN statement followed by ELSE and another single statement is possible in the single line form.
See Also: