11.6 Switch-Case statement

 
Switch <ex1> Case <ex2>[:] <blk> [...] [Default[:] <blk>] EndSwitch

The Switch statement can be used to compare the expression specified in ex1 with all other expressions specified after the Case identifiers. You can use as many Case statements as you want but there must be at least one Case identifier in your Switch statement. If the expression after a Case matches ex1, the code after that Case statement is executed. After the execution, Hollywood continues with your program after the EndSwitch statement. If none of the Case expressions match ex1, the code following the Default statement will be executed. Note that using the Default statement is optional. If you do not need it, you do not have to use it. If you do use it, however, it always needs to be the last statement of the Switch statement. It isn't allowed to have additional Case statements after the Default statement. The colon after the Case and Default statements is optional too.

Please note that the expression following the Case statement must always be constant. You cannot use variables or function return values here. Also you must not mix variable types in this statement: If ex1 is a string, all other expressions must be strings too. If ex1 is a number, all other expressions must be numbers as well.

Here is an example:

 
Switch x
Case 1:
    DebugPrint("x = 1")
Case 2:
    DebugPrint("x = 2")
Default:
    DebugPrint("x <> 1 And x <> 2")
EndSwitch

The above code looks at the variable x and enters the first Case statement if x is one. The second Case statement is entered if x is 2. Otherwise the Default statement is entered.

Every Switch-Case statement can also be written as a normal If statement. The example from above would look like the following then:

 
If x = 1
    DebugPrint("x = 1")
ElseIf x = 2
    DebugPrint("x = 2")
Else
    DebugPrint("x <> 1 And x <> 2")
EndIf

C and Java programmers should note that Hollywood's Switch statement does not automatically fall through to the next Case block after reaching the end of the previous Case block. Instead, Hollywood will automatically jump to the end of the statement after a Case block has been executed. Thus, you do not have to use Break at the end of a Case block either. But you can use it earlier to exit from the Switch statement.

It is, however, possible to manually force a fall through using the FallThrough statement. Whenever Hollywood encounters this statement inside a Case block, it will fall through to the next Case block (or the Default block), i.e. it will jump directly into this block. Therefore, FallThrough may only be used if there is a Case or Default block following. Otherwise an error will be generated. Since the Default block must always be the last block of a Switch statement, it is not allowed to use FallThrough in the Default block because there is no subsequent block to fall through to.

Here is an example:

 
Switch msg.action
Case "OnKeyDown":
    FallThrough
Case "OnKeyUp":
    DebugPrint("Key event:", msg.key)
Default:
    DebugPrint("Other event")
EndSwitch

The code above will print the key that has been pressed in case msg.action is OnKeyDown or OnKeyUp. If msg.action is OnKeyDown, the FallThrough statement is used to jump into the OnKeyUp block.

Alternatively, the FallThrough statement can also be placed directly after the expression after the Switch statement. In that case, each Case block will automatically fall through to the next one, unless there is a Break statement forbidding falling through.

Here is an example:

 
Switch msg.action FallThrough
Case "OnKeyDown":
Case "OnKeyUp":
    DebugPrint("Key event:", msg.key)
    Break
Case "OnMouseDown":
Case "OnMouseUp":
    DebugPrint("Left mouse event")
    Break
Case "OnRightMouseDown":
Case "OnRightMouseUp":
    DebugPrint("Right mouse event")
    Break
Default:
    DebugPrint("Other event")
EndSwitch

The code above uses FallThrough globally to use the same code for OnKeyDown and OnKeyUp, and for OnMouseDown and OnMouseUp, and for OnRightMouseDown and OnRightMouseUp. The Break statements are necessary because otherwise Hollywood would fall through all the way to the very last line of code in the Default block.


Show TOC