Next: , Previous: , Up: Imperative Statements   [Contents][Index] if

Conditional handling of statements is supported by the if, which can have an optional else:

if ::= ("if" "(" bool-expression ")" imperative-statement)
         | ("if" "(" bool-expression ")" imperative-statement
              "else" imperative-statement)

For example

if (idle)
  {world; idle = false;}

Since 2.14.0, a valued call may be used in an if-expression.

For example:

if (bool_function ()) …;
if (!bool_function ()) …;
if (!bool_function () && b) …;
if (enum_function () == result.TRUE) …;
if (enum_function () != result.ERROR || b) …;

Since 2.16.0, arbitrarily complex expressions may be used.

Note that nested ifs are allowed:

if (b1) if (b2) then-statement else else-statement

is interpreted as

if (b1)
{ if (b2) then-statement else else-statement }

In other words: else binds to the closest if.

Note: In an interface, an illegal is not allowed as a then-statement or an else-statement, however the same can be expressed using a guard.