Conditional Statements

    These statements serve to perform different set of statements depending on condition value.

    If Statement

    On the basis of the Condition value, this statement decides whether the Statement should be executed. If the Condition is true, the Statement is executed. If it is false, the Statement is ignored and the process continues next after the if statement. The Statement is either a simple statement or a block of statements:

    • if (Condition) Statement

    Unlike the previous version of the if statement (in which the Statementis executed only if the Condition is true), other Statements that should be executed even if the Condition value is false can be added to the if statement. Thus, if the Condition is true, the Statement1 is executed, if it is false, the Statement2 is executed. See below:

    • if (Condition) Statement1 else Statement2

    The Statement2 can even be another if statement, and also with an else branch:

    • if (Condition1) Statement1
          else if (Condition2) Statement3
              else Statement4

    Example 66.19. If statement

    integer a = 123;
    if ( a < 0 ) {
        a = -a;

    Switch Statement

    Sometimes you would have very complicated statement if you created the statement of more branched out if statement. In this case, it is much more convenient to use the switch statement.

    Now, instead of the Condition as in the if statement with only two values (true or false), an Expression is evaluated and its value is compared with the Constants specified in the switch statement.

    Only the Constant that equals to the value of the Expression decides which of the Statements is executed.

    If the Expression value is Constant1, the Statement1 will be executed, etc.


    Remember that literals must be unique in the Switch statement.

    • switch(Expression) {
          case Constant1 : Statement1 StatementA [break;]
          case Constant2 : Statement2 StatementB [break;]
          case ConstantN : StatementN StatementW [break;]

    The optional break; statements ensure that only the statements corresponding to a constant will be executed. Otherwise, all below them would be executed as well.

    In the following case, even if the value of the Expression does not equal the values of the Constant1,...,ConstantN, the default statement (StatementN+1) is executed.

    • switch (Expression) {
          case Constant1 : Statement1 StatementA [break;]
          case Constant2 : Statement2 StatementB [break;]
          case ConstantN : StatementN StatementW [break;]
          default : StatementN+1 StatementZ

    Example 66.20. Switch statement

    integer ok = 0;
    switch ( response ) {
        case "yes":
        case "ok":
            a = 1;
        case "no":
            a = 0;
            a = -1;