NXC  Version 1.2.1 r5
 All Data Structures Files Functions Variables Groups Pages
The switch statement

A switch statement executes one of several different code sections depending on the value of an expression.

One or more case labels precede each code section. Each case must be a constant and unique within the switch statement. The switch statement evaluates the expression, and then looks for a matching case label. It will execute any statements following the matching case until either a break statement or the end of the switch is reached. A single default label may also be used - it will match any value not already appearing in a case label. A switch statement uses the syntax shown below.

switch (expression) body

Additional information about the case and default labels and the break statement can be found below.

A typical switch statement might look like this:

case 1:
// do something when x is 1
case 2:
case 3:
// do something else when x is 2 or 3
// do this when x is not 1, 2, or 3

NXC also supports using string types in the switch expression and constant strings in case labels.