switch statement
The switch construction allows you to perform different operations in case an expression has different values. The switch keyword is followed by the initial expression that is calculated and stored as the switch value. Then you enumerate case constructions in curly braces with all possible values and the source code that should be executed. One case can have several possible values separated with a comma in case of which it will be executed. After executing the case block with the matching value , the program goes to the statement coming after switch. The rest of case blocks are not checked.
switch a + b
{
case 0, 1, 2
{ ... }
case 3
{ ... }
case 4,10,12
{ ... }
}
If you want to execute some operations in case none of the case blocks is executed, insert the default construction at the end of switch. The default statement can appear only once and should come after all case statements.
switch ipar
{
case 2,4,8,16,32
{ ... }
case k, k + l
{ ... }
default
{
...
}
}
Additional features
The switch construction can be used not only for numeric expressions, but also for any types supporting the comparison operation ==.
The same as case, it is possible to use the label label for an unconditional jump inside switch. Labels that appear after the case keyword enable you to enter the appropriate case case-block from another case-block.
switch name
{
case "John", "Steve"
label a0
{
...
}
case "Laura", "Vanessa"
{
...
if name == "Laura" : goto a0
}
default
{
...
}
}