The switch
statement in C language is a control flow statement that allows a variable to be tested for equality against a list of values (known as cases). It provides a more readable and organized way to handle multiple conditions compared to using multiple if-else
statements, especially when dealing with a single variable that can take on different constant values.
Syntax of switch
Statement
switch (expression) {
case constant1:
// Code to execute if expression matches constant1
break;
case constant2:
// Code to execute if expression matches constant2
break;
// Additional cases...
default:
// Code to execute if expression does not match any case
}
Components of the switch
Statement
expression
: The variable or expression whose value will be compared against the cases. It must evaluate to an integral type (such asint
,char
, orenum
).case constant
: Each case represents a possible value for the expression. If the expression matches a case's constant value, the corresponding block of code is executed.break
Statement: This statement is used to exit theswitch
block. Without abreak
, the program will continue to execute the subsequent cases until it encounters abreak
or the end of theswitch
block (this is known as fall-through).default
Case: This optional case is executed if none of the specified cases match the expression. It acts as a fallback option.
Example of switch
Statement
Here's an example demonstrating how to use a switch
statement:
#include <stdio.h>
int main() {
int day;
printf("Enter a day number (1-7): ");
scanf("%d", &day);
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 number! Please enter a number between 1 and 7.\n");
}
return 0;
}
Explanation of the Example
- The user is prompted to enter a number corresponding to a day of the week.
- The
switch
statement evaluates the variableday
. - Depending on the value of
day
, the corresponding case block is executed, printing the name of the day. - If the value of
day
is outside the range of1
to7
, thedefault
case is executed, displaying an error message.
Fall-Through Behavior
If you omit the break
statement, the program continues executing the next case(s) even if the case does not match. This is known as fall-through.
Example:
switch (day) {
case 1:
case 2:
case 3:
printf("It's a weekday.\n");
break;
case 4:
case 5:
case 6:
case 7:
printf("It's a weekend.\n");
break;
default:
printf("Invalid day number!\n");
}
In this example, both case 1
, case 2
, and case 3
will result in the same output, which is "It's a weekday."
.
Points to Remember
- The expression in a
switch
statement must evaluate to an integral type (e.g.,int
,char
, orenum
). - Each case must have a unique constant value.
- The
break
statement is essential to prevent fall-through; without it, the code will execute all subsequent cases until abreak
or the end of theswitch
block is reached. - The
default
case is optional but useful for handling unexpected values.
Summary
The switch
statement is a powerful and organized way to handle multiple possible values of a variable. It improves code readability and can make the logic clearer when compared to long chains of if-else
statements.