Switch statement (TADS)

From IFWiki

Syntax

In TADS 2 and TADS 3:

switch ( expression )
{
   case constantExpression-1: 
       statement(s);
       break?;
   case constantExpression-1: 
       statement(s);
       break?;
   . . .
   case constantExpression-n: 
       statement(s);
       break?;
   default:
          statement(s)
}

Note: The case-clauses and the default-clause of a switch-statement are optional.

Example

You want the description of a pear to change based on the number of times the player has taken a bite out of it. You have defined a biteCount variable in your pear object that holds the number of bites taken (0 through 3 bites)

select (biteCount)
{
        case 0:
            "The pear sure looks delicious.";
            break;
        case 1:
            "There's a single bite taken out of the delicious looking pear.";
            break;"
        case 2:
            "The pear is almost gone now.";
            break;
        case 3:
            "The sad core is all that's left of that delicious pear.";
}

Description

The switch statement allows the programmer to test the value of the control variable against multiple values. This makes it easier to code than using several if...else statements. The control variable is used in the opening switch (control-variable), and the following case constantExpression clauses test control-variable against the specific constantExpression.

Related statements: if, break.