The switch statement provides a multiway selection control structure to execute a selected section of code determined by the run-time value of an expression. The condition is an expression which evaluates to an integer value, which include signed, unsiened, char, enum, and even boolean values.
Selection statements are also referred to as conditional statements. Other C selection statements are the if statement and the if-else statement.
The switch statement is used to provide a multiway selection, based on an integral value.
#include <stdio.h>
int main () {
int Count;
Count = 2;
switch (Count) {
case 0:
printf ("None.\n");
break;
case 1:
printf ("Only one.\n");
break;
case 2:
printf ("A pair.\n");
break;
case 3:
printf ("Three.\n");
break;
default:
printf ("Many.\n");
break;
}
return 0;
}
| Break Statement | <break-statement> |
| Statement | <statement> |
| Expression | <expression> |
|
C Now
| C Switch Statement |
| C++ Now | C++ Switch Statement |
| Java Now | Java Switch Statement |
| Pascal Now | Pascal Case Statement |
| PHP Now | PHP Switch Statement |