L3DT users' wiki
Large 3D terrain generator

if

Description

Test the value of a condition, and if true, execute a statement block.

Statement prototype

if <bool:condition>
  ... 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 <islt i 0>
  echo "negative number"
elseif <iseq i 0>
  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 <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

See also

 
plugins/general/zeoscript/reference/functions/if.txt · Last modified: 2017/08/31 05:30 (external edit)
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki