Switch Statement in C
A switch statement is a control flow statement that allows a program to execute a block of code among several blocks, depending on the value of an expression.
It is often used as an alternative to an if-else statement when a program needs to select among several different actions, based on the value of a single expression.
Syntax for a switch statement in C
Here is the syntax for a switch
statement in C:
switch (expression)
{
case value1:
// code to be executed if expression == value1;
break;
case value2:
// code to be executed if expression == value2;
break;
...
default:
// code to be executed if expression does not match any of the values;
}
The expression is evaluated once and compared to the value specified in each case statement. If a match is found, the code block associated with that case is executed.
If no match is found, the code in the default block is executed (if it is present). The break statement is used to exit the switch statement and transfer execution to the statement following the switch.
When to use switch statement
Here are some guidelines for when to use a switch statement:
When you have a single expression with multiple possible values, and you want to perform different actions depending on the value of the expression.
When you have a series of if-else statements that test the same expression and the else clauses are empty or contain only a single statement.
When the different cases can be easily represented by integer or character values.
Here is an example of when a switch statement might be more appropriate than an if-else statement:
int grade = 85;
// Using an if-else statement
if (grade >= 90)
{
printf("A\n");
}
else if (grade >= 80)
{
printf("B\n");
}
else if (grade >= 70)
{
printf("C\n");
}
else if (grade >= 60)
{
printf("D\n");
}
else
{
printf("F\n");
}
// Using a switch statement
switch (grade / 10)
{
case 10:
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
default:
printf("F\n");
}
Explanation
In this example, both the if-else
and switch
statements achieve the same result, but the switch
statement is more concise and easier to read.
Examples of a switch statement
Example 1
Here is an example of a switch statement in C:
#include <stdio.h>
int main(void)
{
int day = 3;
switch (day)
{
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output:
Explanation
In this example, the value of day is 3, so the code associated with the case 3: statement is executed, printing "Wednesday" to the console.
Example 2
Here is another example of using a switch statement in C:
#include <stdio.h>
int main(void)
{
char command = 's';
switch (command)
{
case 'n':
printf("Move North\n");
break;
case 's':
printf("Move South\n");
break;
case 'e':
printf("Move East\n");
break;
case 'w':
printf("Move West\n");
break;
case 'p':
printf("Pick up item\n");
break;
case 'd':
printf("Drop item\n");
break;
default:
printf("Invalid command\n");
}
return 0;
}
Output:
Explanation
In this example, the switch statement is used to execute different actions based on the value of the command variable.
If the value of command is 's', the code associated with the case 's': statement is executed, printing "Move South" to the console.
If the value of command does not match any of the values in the case statements, the code in the default block is executed, printing "Invalid command" to the console.