We use decision making statements in C to control the order of execution of our program. Switch case in C is one the decision making statements which is mostly used when we have multiple alternatives to choose from.
The syntax of switch statement is:
switch(expression)
{
case value1:
statement_1;
break;
case value2:
statement_2;
break;
//we can have as many cases as we want
case value_n:
statement_n;
break;
default:
default statement; //this is not necessary. It is used only for convenience
}
Following is a simple to help you understand how a switch statement works:
The algorithm of switch statement is as follows:
break
statement is used after that case, we break out of switch otherwise keep executing till we reach the end of switch(because there is no break statement) or we reach another break statement.break
in C LanguageBelow is a program on switch case with break.
Here is the C language tutorial explaining Switch Case → Switch Case in C
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
// Local Variable Definition
char grade;
printf("Enter your grade:\n");
scanf("%c", &grade);
switch(grade)
{
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Keep it up!\n\n");
break;
case 'C':
printf("Well done\nbreak keyword takes execution to exit the switch case\n\n");
break;
case 'D':
printf("You passed\n");
break;
case 'F':
printf("Better luck next time\n");
break;
default:
printf("Invalid grade\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
In the example above,grade is called the check condition and 'A', 'B', 'C', 'D' and 'F' are the case values.
Dry run of the above example:
We input a character in grade variable, here, 'C'. Next, we enter the switch statement. Since grade is a char type, it is valid. Now, we check the value of grade against all case values starting with the first one. 'A' is not equal to 'C' so the statements corresponding to 'A' will not be executed. We check the next one. 'B' is not equal to 'C' so the statements corresponding to 'B' will also not be executed. We check the next one. 'C' is equal to 'C' so we execute the statements corresponding to 'C'. We print "Well done break keyword takes execution to exit the switch case" and then execute the break statement which takes us out of the switch case.
break
in CIf there is no break
statement then the cases after the matched case, other than default will all get executed.
Below is a program on switch case without break
statement.
#include<stdio.h>
int main()
{
printf("\n\n\t\tStudytonight - Best place to learn\n\n\n");
/* Local Variable Definition */
char grade;
printf("Enter your grade:\n");
scanf("%c", &grade);
switch(grade)
{
case 'A':
printf("Excellent\n");
case 'B':
printf("\n\n\nKeep it up!\n\nNo break statement\n\nHence all the case following this(but not the ones above this) except the default case will get executed !\n\n");
case 'C':
printf("\n\n\t\tCase C : Well done !\n\n");
case 'D':
printf("\t\tCase D : You passed!\n\n");
case 'F':
printf("\t\tCase E : Better luck next time\n\n\n");
default:
printf("\t\tDefault Case : Invalid grade\n\n\n");
}
printf("Your grade is %c\n",grade);
printf("\n\n\t\t\tCoding is Fun !\n\n\n");
return 0;
}
Here are some other examples you might come across involving switch case.
int a = 4, b = 6;
float c = 4.5;
char ch1 = 'a', ch2 = 'c';
switch((a * b) % 2) //valid
switch(c) //invalid
switch(ch2 + ch1) //valid
int x = 4;
switch (x) {
case 1:
printf("Choice is 1");
break;
case 2:
printf("Choice is 2");
break;
case 3:
printf("Choice is 3");
break;
}
The above piece of code will not print anything since x
does not match with any of the case values.
#include <stdio.h>
int main() {
int x = 1;
switch(x)
{
case 1:
case 2:
printf("1 and 2\n"); //it will print till a break or default is reached
break;
case 3:
case 4:
printf("3 and 4\n");
break;
default:printf("wrong choice!\n");
break;
}
return 0;
}
1 and 2
switch()
can only contain char
and int
.break
is used to exit from switch statement. It is optional.char
variable is always initialized within single quotes.switch
should result in a constant value otherwise it will be invalid.switch
statements.