Introduction to C Programming
Operators in C Part 1
Operators in C Part 2
Precedence and Associativity Of Operators in C
If else Statements in C
Switch Statement in C
Loops in C
While and Do-While loops in C
Nested loops in C
Loop Control Statements in C
1D Arrays in C
2D Arrays in C
Pattern Printing in 2D arrays
Functions in C
Pointers in C
ASCII Encoding and Typecasting in C
Switch Statement
We have already studied if-else statements. They allow us to execute different statements based
on where some condition is true or false. Switch also allows us to do the same thing but in a
more readable format. If else statements sometimes make code hard to read whereas the syntax
of switch statement is quite clear and easy.
- Syntax of Switch Statement
- Example of Switch Statement
- Rules for writing Switch Statement
Jump to specific sections
Syntax of Switch Statement
The syntax is as follows :
switch(expression/variable)
{
case value1: //Statements
break;
case value2: //Statements
break;
case value3: //Statements
break;
}
- If the value of the switch expression matches any of the case values, then only the statements included in that case will be executed.
- Once a case gets executed the break statement is used to come out of the switch block because we do not want to execute any other statement in the switch once we find a matching case.
Example of Switch Statement
Suppose you want to give remarks to a student
based on his grade. This is how you would do using a switch statement.
#include <stdio.h>
void main()
{
char grade;
printf("Enter your grade : ");
scanf("%c",&grade);
switch(grade)
{
case 'A': printf("Excellent Performance! Keep it up.");
break;
case 'B': printf("Good Performance! You can do more.");
break;
case 'C': printf("Average performance! Work harder next time.");
break;
case 'D': printf("Bad performance! You need to work harder.");
break;
case 'E': printf("You have failed! Work harder next time");
break;
}
}
Output 1
Enter your grade : A
Excellent Performance! Keep it up.
Output 2
Enter your grade : D
Bad performance! You need to work harder.
Rules for writing Switch Statement
There are a certain rules to be followed while using the switch statement.
1. Case value can only be an integer or character.No other data type like float, double or any expression like a >= b or a == b is allowed.
1. Case value can only be an integer or character.No other data type like float, double or any expression like a >= b or a == b is allowed.
| Allowed | Not Allowed |
|---|---|
| case 4 : | case 4.5 : |
| case 'a' : | case r : |
| case 'a' > 'b' : | case a > b : |
2. Every case must be concluded with a break statement except the last one.Although, it would not generate
any error but it would lead to execution of multiple cases as shown below.
#include<stdio.h>
void main()
{
int day;
printf("Enter day number : ");
scanf("%d",&day);
switch(day)
{
case 0: printf("Sunday\n");
case 1: printf("Monday\n");
case 2: printf("Tuesday\n");
case 3: printf("Wednesday\n");
case 4: printf("Thursday\n");
case 5: printf("Friday\n");
case 6: printf("Saturday\n");
}
}
Output
Enter day number : 3
Wednesday
Thursday
Friday
Saturday
3. Switch can have an optional default case. If the value of the switch expression does not match
any of the case values, then this case will be executed.
#include<stdio.h>
void main()
{
int n;
printf("Enter number : ");
scanf("%d",&n);
switch(n)
{
case 1: printf("Number is one.\n");
break;
case 2: printf("Number is two.\n");
break;
case 3: printf("Number is three\n");
break;
default:printf("Number is greater than three\n");
}
}
Output
Enter number : 4
Number is greater than three