Chapter 5: C Control Flow
:
statements
case constant expression31 : statements
default: ments
We can redo the if else if block used in the joystick interrupt example using a
switch
01;
:
x02;
x04;
eak;
case KEY_ENTER :
PORTD = ~0x10;
ou can let cases fall through, which can be handy in circumstances such as
evaluat cter is a capital or
lower c want the same response for a range of integers:
case :
switch (expression) {
case constant expression1 statements
case constant expression2 :
state
}
statement as follows:
switch(input){
case KEY_PLUS :
PORTD = ~0x
break:
case KEY_NEXT
PORTD = ~0
break;
case KEY_PREV :
PORTD = ~0
break;
case KEY_MINUS :
PORTD = ~0x08;
br
break;
default:
}
So if the ‘input’ == KEY_NEXT, then PORTD = ~0x01. The ‘break’ statement
causes an immediate exit from the switch block. If you want to continue
evaluating cases against the input, leave out the break and the subsequent
statements will be looked at.
Y
ing character input where you don’t care if the chara
ase letter, or perhaps you
switch( input){
‘a’
case ‘A’ :
DoaA();
break;
case ‘b’ :