Switch-Case in C
Introduction
In the C programming language, the
switch-case
statement is used to perform different actions based on different conditions. It provides a convenient way to handle multiple cases without using multiple
if-else
statements. The
switch
statement evaluates an expression and compares it with various constant values, executing the block of code associated with the matching value.
Syntax
The basic syntax of the
switch-case
statement in C is as follows:
switch (expression) {
case constant1:
// code to execute when expression matches constant1
break;
case constant2:
// code to execute when expression matches constant2
break;
// more cases can be added
default:
// code to execute when expression doesn't match any constant
}
Working of the switch-case Statement
When the
switch
statement is encountered, the expression is evaluated. The resulting value is then compared with the constant values specified in the
case
labels. If a match is found, the corresponding block of code is executed. The
break
statement is used to exit the
switch
block and prevent the execution of subsequent case blocks.
If no match is found, the code inside the
default
block is executed (if provided). The
default
block is optional and acts as a catch-all case.
Example
Let's look at an example that demonstrates the usage of
switch-case
in C:
#include <stdio.h>
int main() {
int choice;
printf("Enter a number between 1 and 3: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("You entered 1.\n");
break;
case 2:
printf("You entered 2.\n");
break;
case 3:
printf("You entered 3.\n");
break;
default:
printf("Invalid choice.\n");
}
return 0;
}
In this example, the user is prompted to enter a number between 1 and 3. The value is then stored in the
choice
variable. The
switch
statement evaluates the value of
choice
and executes the corresponding block of code based on the matching case. If the user enters 1, 2, or 3, the program displays a corresponding message. Otherwise, if the user enters any other value, the program displays an "Invalid choice" message using the
default
case.
Multiple Cases
In C, multiple cases can share the same block of code. This can be achieved by omitting the
break
statement after each case except for the last one in the group. The code will then fall through to the next case until a
break
statement is encountered.
int day = 3;
switch (day) {
case 1:
case 2:
case 3:
printf("Weekday\n");
break;
case 4:
case 5:
printf("Weekend\n");
break;
}
In this example, if
day
is 1, 2, or 3, the program will output "Weekday." If
day
is 4 or 5, the program will output "Weekend."
Conclusion
The
switch-case
statement in C provides an efficient way to handle multiple cases based on a single expression. It helps avoid nested
if-else
statements and improves code readability. Remember to use the
break
statement after each case to prevent the execution from falling through to the next case block. In next lesson, we'll learn another control flow statements, the iterative statements. Happy coding!