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 Break

    …

    Case 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 will allow any number of Boolean-expressions and statements to be executed. The Case Else and {else-statement} are optional. If it is not present then 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

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

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.