====== if ======
===== Description =====
Test the value of a condition, and if true, execute a statement block.
===== Statement prototype =====
if
... statement block ...
endif
===== Arguments =====
^ 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.|
===== Example =====
int i
set i 5
if
echo "negative number"
elseif
echo "zero"
else
echo "positive number"
endif
===== Comments =====
==== Terminating an 'if' statement block ====
''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.
==== 'Else' after 'if' ====
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
echo "negative number"
else // error, else not terminated by endif
if
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
echo "negative number"
else // now the else is terminated by 'endif', and contents are nested
if
echo "zero"
else
echo "positive number"
endif
endif
**Better:**
if
echo "negative number"
elseif // using the elseif statement, rather than nested else/if
echo "zero"
else
echo "positive number"
endif
===== See also =====
* [[plugins:general:zeoscript:reference:functions:elseif]]
* [[plugins:general:zeoscript:reference:functions:else]]
* [[plugins:general:zeoscript:reference:functions:endif]]