Test the value of a condition, and if true, execute a statement block.
if <bool:condition> ... statement block ... endif
| Name | Type | Comment |
|---|---|---|
| condition | bool | The condition that determines whether the statement block is to be executed. If true, the blcok is executed, and if false, execution skips to the matching endif/elseif/else statement that terminates the statement block. |
int i set i 5 if <islt i 0> echo "negative number" elseif <iseq i 0> echo "zero" else echo "positive number" endif
If statements in ZeoScript will conditionally execute a block of code that is explicitly terminated by an elsif, else or endif statement.
Each if statement must be closed by a single matching endif statement. Optionally, the if statement may be followed by multiple elseif statements, and/or one else statement. The else statement must be the final conditional statement before the terminating endif statement.
ZeoScript differs from C/C++ and some other programming languages in that if, else and elseif statements are explicitly block statements. In C/C++, for instance, you can omit the block { parens } after an if statement if you only want the next statement to be executed. Not so in ZeoScript; all if, else and elseif statements must be explicitly closed.
Wrong:
if <islt i 0> echo "negative number" else // error, else not terminated by endif if <iseq i 0> echo "zero" else echo "positive number" endif
This code will result in the following error:
ZeoScript error: Cannot find matching 'endif' for 'else' statement. - Line 3: 'else' ZeoScript compilation aborted on line 3 following errors.
Right:
if <islt i 0>
echo "negative number"
else // now the else is terminated by 'endif', and contents are nested
if <iseq i 0>
echo "zero"
else
echo "positive number"
endif
endif
Better:
if <islt i 0> echo "negative number" elseif <iseq i 0> // using the elseif statement, rather than nested else/if echo "zero" else echo "positive number" endif