C Switch statements
The switch statements are used in the scenario where you need many if statements. In Switch statements, you will be choosing from multiple options. There can be multiple cases in the switch statements with the break statement in each case. If you are not using the break statement, then the next case value will be evaluated for the condition and the code will be executed.
Syntax
switch(variable)
{
case 1:
….statement…
break;
case 2:
….statement…
break;
.
.
case n:
….statement…
break;
default:
….statement…
break;
}
Example Program
#include
void main()
{
int a;
printf("Please enter a no between 1 and 4: ");
scanf("%d",&a);
switch(a)
{
case 1:
printf("You chose the number 1");
break;
case 2:
printf("You chose the number 2");
break;
case 3:
printf("You chose the number 3");
break;
case 4:
printf("You chose the number 4");
break;
default :
printf("Enter a no between 1 and 5");
break;
}
}
Output
Please enter a no between 1 and 4: 3
You chose the number 3