Statements: switch case default
The switch-case statement allows conditional execution of statements.
Syntax
switch(expression){casecase‑expression: statementdefault:statement}
Discussion
The switch statement evaluates the given expression, looks for the first case clause with the case-expression matching the value of the evaluated expression, and executes the statement associated with that case. If no matching case clause is found, the statement associated with the default clause is executed. A switch statement may contain any number of case clauses but only one default clause.
switch statements are useful to control complex conditional and branching operations, which would otherwise only be possible through the use of nested if-else statements. In the below example, depending on the actual value of the local variable keyCode the switch statement selects and executes one of the direction = ... assignment operations. If keyCode contains a value not listed in any of the case clauses, the empty statement in the default clause is executed:
var Core::KeyCode keyCode = ...; // The code of a pressed key var Core::Direction direction = Core::Direction.None; // Depending on the pressed key, determine the direction to navigate in // the GUI application switch ( keyCode ) { case Core::KeyCode.Left : direction = Core::Direction.Left; case Core::KeyCode.Right : direction = Core::Direction.Right; case Core::KeyCode.Up : direction = Core::Direction.Top; case Core::KeyCode.Down : direction = Core::Direction.Bottom; default :; }
The switch statement expects the expression to result in a one of the following data types: user defined enumeration, language, bool, char, signed or unsigned integer. Accordingly, all case-expressions have to be constant values matching exactly the same data type.
Despite the similarity with the programming languages C, C++ or JavaScript, the Chora version of the switch statement has its own peculiarities. First at all, in Chora the program execution doesn't fall through to the next case or default clause. The keyword break known from C, C++ or JavaScript is thus not necessary. To achieve the fall through use the fallthrough statement explicitly as shown in the example below:
switch ( CharCode ) { case 'A', 'B', 'C' : { trace "Upper case letter"; fallthrough; } case 'a', 'b', 'c' : { trace "A letter"; fallthrough; } default: trace "Any sign"; }
Furthermore, each case and default clause expects exactly one statement. To execute multiple statements, you can group those together within a block statement. On the other hand, the Chora version of the switch statement allows you to specify multiple constant values for one and the same case clause. The values are simply separated by the , (comma) sign. For example, the following code distinguishes between vertical or horizontal direction only:
var Core::KeyCode keyCode = ...; // The code of a pressed key var bool verticalDirection = false; var bool horizontalDirection = false; // Depending on the pressed key, determine the direction to navigate in // the GUI application switch ( keyCode ) { case Core::KeyCode.Left, Core::KeyCode.Right : horizontalDirection = true; case Core::KeyCode.Up, Core::KeyCode.Down : verticalDirection = true; default :; }