Control Flow

 

Conditional statements of the following form are supported:

 

if (expression) statement

if (expression) statement else statement

 

If expression is non-zero, statement is evaluated. If expression is zero, the optional else statement is evaluated.

 

For example:

 

if (a < 10) echo(a);

if (a < 10) echo(a);

else echo("FALSE");

 

Compound statements must be enclosed in { }. Example:

 

if (a < 10)

{

    b = 1;

    c = 2;

}

else

{

    b = 0;

    c = 0;

}

 

Multiple else-if clauses are supported:

 

if (a < 10)

{

    b = 1;

}

else if (a < 20)

{

    b = 2; 

}

else

{

    b = 3;

}

 

The ternary conditional acts as a concise if-else statement:

 

 var = (expression) ? statement1 : statement2

 

Is equivalent to:

 

if (expression)

{

    var = statement1;

}

else

{

    var = statement2;

}

 

For example:

 

a = (b > c) ? b : c + 10;

 

The switch statement causes control to be transferred to one of several statements depending on the value of an expression.

 

switch (expression) statement

 

Unlike C/C++, expression may evaluate to an integer, real or string value. The statement is typically compound and is labeled with a case prefix as follows:

 

case constant-expression:

 

Each case constant must be unique. The optional default labeled statement is executed if expression does not compare to any case constant.

 

switch (x)

{

    case 10:

    case 22.5:

        a = 1;

        break;

 

    case "text":

        a = 2;

        break;

 

    default:

        a = 0;

        break;

}