Switch Statement In C Programming Language

Switch Statement In C Programming Language

Switch statement is an alternate of if-else-if ladder statement. It allows us to execute multiple operations for the different possible values of a single variable.

Syntax:

    switch(expression){    
case value1:
//code to be executed;
break; //optional
case value2:
//code to be executed;
break; //optional
......

default:
code to be executed if all cases are not matched;
}

Rules for switch statement


1) It is must be of an integer or character type.

2) Case value must be an integer or character constant.

3) Break statement is optional. If there is no break statement found in the case, all the cases will be executed until it got any Break statement

Switch Statement In C Programming Language

Flowchart of switch statement


Switch Statement In C Programming Language

Following is a simple example of switch statement

    #include<stdio.h>  
int main(){
int number=0;
printf("enter a number:");
scanf("%d",&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;
}

After executing the program our output looks like

enter a number:4
number is not equal to 10, 50 or 100

enter a number:50
number is equal to 50

Let’s see another example

    #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 ");
}

}

After executing the program our output looks like

hi 

Fall-through Switch statement

Fall-through means if we don’t use a break statement, all the cases after the matching case will be executed. Let’s see a fall-through example

    #include<stdio.h>  
int main(){
int number=0;

printf("enter a number:");
scanf("%d",&number);

switch(number){
case 10:
printf("number is equal to 10n");
case 50:
printf("number is equal to 50n");
case 100:
printf("number is equal to 100n");
default:
printf("number is not equal to 10, 50 or 100");
}
return 0;
}

After executing the program our output looks like

enter a number:10
number is equal to 10
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100

enter a number:50
number is equal to 50
number is equal to 100
number is not equal to 10, 50 or 100

Nested switch case statement

We can use as many switch statement inside a switch statement. This type of statements is called nested switch statements. Let’s see a nested switch case 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: %dn",i);
case 20:
switch(j) {
case 20:
printf("The value of j evaluated in nested switch: %dn",j);
}
}

printf("Exact value of i is : %dn", i );
printf("Exact value of j is : %dn", j );

return 0;
}

After executing the program our output looks like

the value of i evaluated in outer switch: 10
The value of j evaluated in nested switch: 20
Exact value of i is : 10
Exact value of j is : 20
Post a Comment