Sequential structure
A script consists of a sequence of commands or instructions. These instructions are executed one after the other in the order that they are specified. Sometimes, you may want to deviate from this sequential structure, under certain conditions.

Selective structure
A selective structure allows you to make the execution of an instruction (or group of instructions enclosed in curly brackets { .... }) dependent of a specified condition. Only if the condition is fulfilled will the instruction be executed, otherwise the instruction is ignored and passed over.

int $height = 2;
int $depth;
if ($height > 2) {
  $depth = $height;
  polyCube -w 1 -h $height -d $depth;
}
if ($depth < 1) {
  polySphere -r 1;
}

Explanation:

  • The variable $height is initialized to the value 2.
  • The variable $depth is not initialized, it receives the default value 0.
  • If the value of $height is greater than 2, then the value of $depth will be set equal to the value of $height and a cube will be created. However, the value of $height is only two and these instructions are ignored.
  • Subsequently, if the value of $depth is less than 1, a sphere is created. Since $depth received the default value 0 and its value hasn't changed since, the sphere is created.

    The if-else construct
    The if-construct above does nothing but ignore the instructions when the condition is not fulfilled. The if-else construct allows you to specify an alternative instruction (or group of instructions) that will be executed only if the condition is not fulfilled.

    int $height = 2;
    int $depth;
    if ($height > 2) {
      $depth = $height;
      polyCube -w 1 -h $height -d $depth;
    } else {
      polySphere -r 1;
    }

    Conditional operators
    To check the equality of two values, you must use the double equality sign ==. The single equality sign = is the assignment operator.

    SymbolMeaning
    <smaller than
    >greater than
    <=smaller than or equal to
    >=greater than or equal to
    ==equal to
    !=not equal to

    Logical operators
    You can combine two or more conditions (each enclosed in parentheses) using the logical operators AND, OR and NOT.

    SymbolNameMeaning
    &&ANDboth conditions must be fulfilled
    ||ORone or the other condition must be fulfilled
    !NOTthe condition may not be fulfilled

    Example:
    int $height = 2;
    int $depth;
    if (($height > 2) && !($depth < 1)) {
      $depth = $height;
      polyCube -w 1 -h $height -d $depth;
    } else {
      polySphere -r 1;
    }