Skip to content

Case

See Also: Execution Control Commands, If command, If function

Purpose

Allows conditional execution of code based on one or more Boolean expressions.

Syntax

Case Begin
    Case {Boolean-expression1}
        {statement1}
        Case Break
    Case {Boolean-expression2}
        {statement2}
        Case Break
    Case {Boolean-expression3}
        {statement3}
        Case BreakCase Else
        {else-statement}
Case End

What It Does

If {Boolean-expression1} is True, then {statement1} is executed. If {Boolean-expression2} is True, then {statement2} is executed. If {Boolean-expression3} is True, then {statement3} is executed, and so on. If none of the Boolean expressions are true, then the {else-statement} is executed.

The Case statement allows any number of Boolean expressions and statements to be executed. The Case Else and {else-statement} are optional. If they are not present, execution will continue at the next statement in the program when all the Boolean expressions are False.

It is normal to end each case with a Case Break command. The Case Break command will immediately halt execution of the Case statement, and program execution will continue at the next statement in the program (following the Case statement). If you do not end each case with a Case Break, then execution will continue down through all the cases until a Case Break is executed or the Case statement ends.

Example 1

Case Begin
    Case (sAnimal = "Cat")
        Move ("Meow") To sNoise
        Case Break
    Case (sAnimal = "Dog")
        Move ("Arf") To sNoise
        Case Break
    Case (sAnimal = "Bird")
        Move ("Tweet") To sNoise
        Case Break
    Case Else
        Move ("") To sNoise
Case End

Example 2

String sAnimal
Move "Dog" To sAnimal
Case Begin
    Case (sAnimal = "Cat")
        Showln "Meow"
    Case (sAnimal = "Dog")
        Showln "Arf"
    Case (sAnimal = "Bird")
        Showln "Tweet"
    Case Break
    Case Else
        Showln "or else"
Case End

The above sample demonstrates a common mistake in implementing the Case command.

The first Showln statement will not be executed. The Showln statements will start executing after the second case, which is the first case that evaluates to true, until a Break (or a Case End) is found.

The result of this case will be that "Arf" and "Tweet" will be displayed because the second case (Dog) is missing a Case Break statement.