The switch
statement in C is an alternate to if-else-if
ladder statement. It allows a single variable to be compared with several possible constants. It is a multi-way decision that tests whether an expression matches one of the constants(constant integer values), then a execution jump is made to that point. A constant can not appear more than once, and there can only be one default expression.
The syntax of switch
statement in is given below:
switch (expression) { case const1: //statement1 (code to be executed) break; //optional case const1: //statement2 (code to be executed) break; //optional ...... ...... default: statements(code to be executed if all cases are not matched) }
On evaluating the expression, if it matches one of the set of constant integer expressions, the switch branches to the matching case
label and executes the statements following that point. Otherwise it jumps to the default
label and executes its statements.
The statements following a case
label are executed until terminated by a break
statement, which causes an immediate exit from the switch block. If a break
is not encountered, execution will flow on through to the next cases until the end of the block. It is termed fall through and is the default behaviour in a switch.
default
label is optional, and if it does not exist, and none of the case labels match, the switch
simply performs no action.
switch
expression must be of an integer or character type.case
value must be an integer or character constant.Let's see a simple example of c language switch statement.
#include<stdio.h> int main() { int number = 0; printf("enter a number:"); scanf("%d", & amp; number); switch (number) { case 10: printf("number is equals to 10"); break; case 50: printf("number is equal to 50"); break; case 100: printf("number is equal to 100"); break; default: printf("number is not equal to 10, 50 or 100"); } return 0; }
#include <stdio.h> int main() { int x = 10, y = 5; switch(x>y && x+y>0) { case 1: printf("hi"); break; case 0: printf("bye"); break; default: printf(" Hello bye "); } }
In C language, the switch statement is fall through; it means if you don't use a break statement in the switch case, all the cases after the matching case will be executed.
Let's try to understand the fall through state of switch statement by the example given below.
#include<stdio.h> int main() { int number = 0; printf("enter a number:"); scanf("%d", & amp; number); switch (number) { case 10: printf("number is equal to 10\n"); case 50: printf("number is equal to 50\n"); case 100: printf("number is equal to 100\n"); default: printf("number is not equal to 10, 50 or 100"); } return 0; }
We can use as many switch statement as we want inside a switch statement. Such type of statements is called nested switch case statements. Consider the following example.
#include <stdio.h> int main () { int i = 10; int j = 20; switch(i) { case 10: printf("the value of i evaluated in outer switch: %d\n",i); case 20: switch(j) { case 20: printf("The value of j evaluated in nested switch: %d\n",j); } } printf("Exact value of i is : %d\n", i ); printf("Exact value of j is : %d\n", j ); return 0; }