How to use switch statement in C Programming

 

Switch statement is also called as multiple selection structure in C Programming. Switch selection structure is similar to if-else selection structure. The difference is instead of using multiple if-else statement we use ‘cases’ to check for multiple conditions. The ‘default’ case will run when no condition is true. The following simple c program explains how to use switch statement in c programming.

[js]

#include <stdio.h>

int main(void){
int number1; //defined variable named as number1

printf("Please Enter Integer Number:n");
scanf("%d", &number1); // getting variable value from user and assiging value to variable named as number1
printf("n");

switch(number1){ // expression to check is written in parenthesis
case 10: // it’s similar to: if (number1 == 10)
{
printf("your entered number is 10n");
break; // break statement is used to terminate the switch statement
}
case 20: // it’s similar to: if (number1 == 20)
{
printf("your entered number is 20n");
break; // break statement is used to terminate the switch statement
}
case 30: // it’s similar to: if (number1 == 30)
{
printf("your entered number is 30n");
break; // break statement is used to terminate the switch statement
}
default: // the default case will run when none of the above case is true
{
printf("you entered number other than 10,20, or 30");
break; // break statement is used to terminate the switch statement
}
}
[/js]

Output:

One thought on “How to use switch statement in C Programming

Leave a Reply to ali raza Cancel reply

Your email address will not be published.