Case - of - other

Case - of - other (Advanced)

Top  Previous  Next

Often a program must decide between more than the two alternatives offered by an "if" statement. Since an "if" statement can contain other statements, "if" statements can be nested. For example:

 

        if Guess = Number then Text(0, "Correct!!")

        else if Guess < Number then Text(0, "Too low")

        else if Guess > 100 then Text(0, "Way too high")

        else Text(0, "Too high")

 

However many levels of nested "if" statements can be inefficient and confusing, so XPL0 has the "case" statement.

 

The "case" statement has two forms, the first is:

 

        case of

                BOOLEAN EXPRESSION: STATEMENT;

                BOOLEAN EXPRESSION: STATEMENT;

                ...

                BOOLEAN EXPRESSION: STATEMENT

        other STATEMENT

 

In this form the "case" statement is like the nested "if"s shown above. The first expression that evaluates to true causes the corresponding statement to be executed. If no expression is true then the "other" statement is executed. Note that there is no semicolon before "other". The nested "if" example translates as follows:

 

        case of

                Guess = Number: Text(0, "Correct!!");

                Guess < Number: Text(0, "Too low");

                Guess > 100:    Text(0, "Way too high")

        other Text(0, "Too high")

 

The "other" cannot be left out, but it can have a null statement:

 

        case of

                Number = 1: DoOne;

                Number = 2: DoTwo

        other   [];

 

The second form of the "case" statement is used for efficiency. The expressions must all have a common component and must be a comparison for equality, like in the last example. This form is:

 

        case EXPRESSION of

                EXPRESSION: STATEMENT;

                EXPRESSION: STATEMENT;

                ...

                EXPRESSION: STATEMENT

        other STATEMENT

 

The last example, in this form, looks like this:

 

        case Number of

                1: DoOne;

                2: DoTwo

        other   [];

 

Sometimes several different expressions are associated with a single statement. For example:

 

        case Number of

                1: DoOdd;

                2: DoEven;

                3: DoOdd;

                4: DoEven;

                5: DoOdd

        other   [];

 

Here, if Number equals 1, 3, or 5 then the subroutine DoOdd is executed; if Number equals 2 or 4 then DoEven is executed. The "case" allows any number of expressions to select a statement. The form is:

 

        EXPRESSION, EXPRESSION, ... EXPRESSION: STATEMENT

 

So, the example above could be rewritten:

 

        case Number of

                1,3,5:  DoOdd;

                2,4:    DoEven

        other   [];

 

"Case" expressions must evaluate to integers. Reals cannot be used since it is generally a coincidence when two reals are exactly equal. However, a comparison containing reals, such as 2.3 > X, evaluates to true or false, which is an integer expression that can be used by the first "case-of" form.

 

Note that "case" selectors are not limited to simple constants; they can be any integer expression.