Conditional statements (if-else) can be used to program decision making on which actions to perform

Syntax

The keyword if is used for a simple conditional statement:

if (expression) statement-true;

In this case when the expression is true then statement-true will be executed.

An if-else construction can be used for programming more elaborate decisions.

if (expression) statement-true;
else statement-false;

if the expression is true then statement-true will be executed, else statement-true is skipped and statement-false is executed.

Examples

A basic if-else example

This example shows an interface with a behaviour section where if else is used to select the actions to be performed based on the value of a state and a local variable.

interface IImperativeAction
{
  in void doIt();
  out void done();
  out void skip();

  behaviour
  {
    enum Toggle {Ping, Pong};
    Toggle state = Toggle.Ping;

    on doIt:
    {
      bool localVariable = false;

      if (state.Ping)
      {
        localVariable = true;
      }
      else
      {
        localVariable = (state == Toggle.Ping);
      }

      if (localVariable)
      {
        done;
        state = Toggle.Pong;
      }
      else
      {
        skip;
        state = Toggle.Ping;
      }
    }
  }
}

See also: