mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Upon finding a match, program flow continues normally: following instructions will be executed in natural order regardless of the possible case label. If no case satisfies the condition, the default case evaluates (if the label default is speci- fied).

For example, if variable i has value between 1 and 3, following switch would always return it as 4:

switch (i) {

case 1: i++;

case 2: i++;

case 3: i++;

}

To avoid evaluating any other cases and relinquish control from the switch, terminate each case with break.

Conditional switch statements can be nested – labels case and default are then assigned to the innermost enclosing switch statement.

Here is a simple example with switch. Let’s assume we have a variable with only 3 different states (0, 1, or 2) and a corresponding function (event) for each of these states. This is how we could switch the code to the appopriate routine:

switch (state) {

case 0: Lo(); break;

case 1: Mid(); break;

case 2: Hi(); break;

default: Message("Invalid state!");

}

page

 

118

MikroElektronika: Development tools - Books - Compilers