Advanced testing with ‘switch’

Listing multiple conditions in an if…then…else statement can prove tedious and messy, as the following example shows:

if \bottle\capacity = 75 then

  {

  "bottle"

  }

if \bottle\capacity = 150 then

   {

   "magnum"

   }

if \bottle\capacity = 300 then

   {

   "jeroboam"

   }

if \bottle\capacity = 600 then

   {

    "maalem"

   }

if \bottle\capacity = 900 then

   {

   "salmanazar"

   }

if \bottle\capacity = 1200 then

   }{

   }"balthazar"

   }}

if \bottle\capacity = 1500 then

   }{

   }"nabuchodonosor"

   }}

As an alternative to multiple and unreadable if…then…else statements, you can use a switch statement. This statement activates different sets of statements depending on the value of a certain expression. If you rewrite the preceding example with a switch statement, the expression would be the following:

switch \bottle\capacity
{
    case 75:
        { "bouteille" }
    case 150:
        { "magnum" }
    case 300:
        { "jeroboam" }
    case 600:
        { "maalem" }
    case 900:
        { "salmanazar" }
    case 1200:
        { "balthazar" }
    case 1500:
        { "nabuchodonosor" }
}

This expression returns the correct name of the Champagne bottle size provided the input capacity falls within the list 75, 150, 300, 600, 900, 1200, 1500. Of course if \bottle\capacity is set to none of these values, the switch statement does not evaluate any of the sets of statements within its structure.

To make sure that the integration engine evaluates at least one set of statements in a switch statement, you can add a default case at the very end:

switch \bottle\capacity
{
    case 75:
        { "bouteille" }
. . .
    case 1500:
        { "nabuchodonosor" }
    default:
        { "unknown size" }
}

The integration engine evaluates the set of statements that is bound to this default case when none of the other cases matches the value of the switch expression.

You often use the switch statement to check whether an expression exactly matches a specific value such as the number 1500 or the string "yes". But, sometimes you might want to evaluate a set of statements if the expressions fall within a set of individual values similar to 3, 19 or 27 or a range of values, such as any number between 1 and 16. In such situations, you can list all the possible values on a single case, or connect the bounds of a range with the reserved word too.

switch \bottle\capacity
{
    case 75, 150:
        { "available, usually" }
    case 300 to 600, 1200:
        { "only upon order" }
    case 900, 1500:
        { "too expansive" }
    default:
        { "not known at all" }
}

Make sure that the value you are evaluating cannot satisfy more than a single case, as happens in the following example:

switch \bottle\capacity
{
    case 75, 150, 400:
        { "available, usually" }
    case 300 to 600, 1200:
        { "only upon order" }
    case 900, 1500:
        { "too expansive" }
    default:
        { "not known at all" }
}

The number 400 is bound to either the first or second set of statements (400 is in the range 300 to 600). If the tested capacity is 400, the expression is always returns the value "available, usually". This is because the case 75, 150, 400 is tested first, preventing the set of statements bound to 300 to 600, 1200 from getting a chance to be evaluated at all.

Testing using switch can involve any type of value that has literals (meaning all classes except V). Promotions are applied whenever required and ranges of values are legal even on booleans, dates and strings: all of these are ordered (false < true…).

However, symbolic values (null and absent) cannot be used as cases values.

Related Links