Menu

keywords-case

Will Pittenger

Overview

A switch statement is broken up into one or more case blocks and an else block. Each case block has a condition associated with it. What can condition can be would depend on what's listed in the switch's own condition segment. Each case block can be empty (and part of the next case) or such that it runs code. If it has code, it must end with a break statement. For more on switch statements, see [Switch statements].

The switch keyword can be by itself, with an identifier, or with an identifier and a comparison operator. The comparison operator can be <, <=, =<, =>, >=, or >. Each possibility is shown below.

A switch with no identifier

This type of switch works a lot like a if/else if. The case blocks don't need to have a condition in common with any other case block.

\switch\
  ' Note: Other cases might be listed as common (sharing the same code and break statement) as this case.  Those aren't shown.
  (\case\ /BooleanCondition/
    ' Run some code
    \break\ )...cases

  \else\
    ' Do something in the else block.

/BooleanCondition/ can be any expression that evaluates to a boolean (true or false) value.

A switch followed by an identifier, but no comparison operator

Some switch statements will always have the same variable on the left side of the comparison. ASIL lets you move that variable up to the switch keyword. Depending on what's next to the case keyword in each case block, this type of switch statement is closest to what is allowed in C++ and C#.

With this type of switch, each case block's condition can have use the comparison operators <, <=, =<, =>, >=, or >—or no comparison operator at all. If no comparison operator is present, a test based on equality will be used. If the equality test is needed and the type is a reference type, ASIL will compare the references unless the type implements either IComparable or IEqualable. If a comparison operator is listed for a case, it must be valid for that type.

A single switch statement of this type can mix and match case block comparison types as needed. However, one block with using an explicit comparison operator reference might hide the next. If the case is something like "< 5" and a later case is "< 2", the second case will never execute.

Unlike C++ and C#, the type doesn't have to be a primitive. Nor the the values in the case block conditions need to be constant.

\switch\ /VariableReference/
  ' Note: Other cases might be listed as common (sharing the same code and break statement) as this case.  Those aren't shown.
  (\case\ [(\<\ | \<=\ | \=<\ | \=>\ | \>=\ | \>\) /ConstantOrVariableReference/
    ' Run some code
    \break\ )...cases

  \else\
    ' Do something in the else block

/VariableReference/ can be any l-value. /ConstantOrVariableReference/ can be any r-value.

A switch statement followed by an identifier and a comparison operator

A switch statement of this type is the same as the previous type, but all the cases use the same comparison operator and that can't be the equality comparison operator. Any type can be used as long as the specified operator is valid for that type.

\switch /variableReference/ (\<\ | \<=\ | \=<\ | \=>\ | \>=\ | \>\)
  (\case\ /ConstantOrVariableReference/
    ' Run some code
    break )...cases

  \else\
    ' Do something in the else block

Related

Wiki: Keywords
Wiki: Switch statements
Wiki: keywords-break
Wiki: keywords-else
Wiki: keywords-switch