switch
switch is C's equivalent of Pascal's case construction.
|
int option;
/* .. */
switch (option) {
case 1:
printf("option 1 selected");
/* .. */
break;
case 2:
printf("option 2 selected");
break;
default:
printf("unknown option selected");
break;
}
|
option must be a numerical type, and that a
break statement must be used if you do not
want control to `fall through' to the following cases:
|
int option;
/* .. */
switch (option) {
case 1:
printf("option 1 selected");
case 2:
printf("option 2 selected");
/* .. */
}
|
option is one, both messages will be displayed.