C - switch-case
In C, the switch-case
statement is a control structure used for multiple conditional branching. It provides a way to select one branch of code execution from several possible options based on the value of an expression. The switch
statement evaluates the expression and compares it to various case values to determine which block of code to execute.
Here's the general syntax of the switch-case
statement in C:
switch (expression) {
case value1:
// Code to execute if expression equals value1
break;
case value2:
// Code to execute if expression equals value2
break;
// Additional cases as needed
default:
// Code to execute if expression does not match any case
}
-
expression
is the value that is evaluated and compared with the case values.
-
case value1
, case value2
, etc., represent the possible values that expression
can match. If a match is found, the corresponding block of code is executed.
-
break
is used to exit the switch
statement once a match is found. If break
is not used, execution will continue to subsequent cases until a break
statement is encountered or until the end of the switch
block.
-
default
is an optional case that is executed when none of the previous cases match the expression. It is like the "default" option for handling unmatched cases.
Here's an example that uses switch-case
to determine the day of the week based on a numeric input:
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): ");
scanf("%d", &day);
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
case 4:
printf("Wednesday\n");
break;
case 5:
printf("Thursday\n");
break;
case 6:
printf("Friday\n");
break;
case 7:
printf("Saturday\n");
break;
default:
printf("Invalid input\n");
break;
}
return 0;
}
In this example:
-
The program asks the user to input a number (1-7).
-
The
switch
statement evaluates the value of day
and compares it to the cases listed from '1' to '7'. When a match is found, the corresponding day name is printed.
-
If the input value does not match any of the cases, the
default
case is executed, and it prints "Invalid input."
Here's the output of the aobve "switch-case" example that determines the day of the week based on a numeric input:
Enter a number (1-7): 3
Tuesday
In this example, when the user entered '3', the program correctly determined that it corresponds to "Tuesday" and displayed "Tuesday" as the output.
The switch-case
statement is especially useful when you have multiple possible values to compare against a single expression, as it provides a more concise and structured way to handle such scenarios.
Scenario when to use "if statement" and when to use "switch-case"?
The choice between using an "if statement" (if-else
or if-else if
) and a "switch-case" (switch
) statement depends on the specific requirements and structure of your code. Here are some guidelines to help you decide when to use each:
Use "if statement" (if-else or if-else if) when:
-
You Need to Test Multiple Conditions: If you have multiple conditions to evaluate, and these conditions are not simply comparing a single expression to different values, then
if-else
is a better choice. You can use multiple if
and else if
statements to handle a variety of conditions sequentially.
if (condition1) {
// Code for condition1
} else if (condition2) {
// Code for condition2
} else {
// Default code
}
-
Conditions Are Complex or Expressions: When conditions involve complex expressions or logical combinations of variables,
if
statements provide more flexibility and readability.
if (x > 10 && (y < 5 || z == 0)) {
// Complex condition handling
} else {
// Default code
}
-
You Need to Handle Non-Integral Values:
if
statements are suitable for handling conditions involving non-integral values, such as floating-point numbers or strings.
-
Values Need to Be Ranged or Calculated: If conditions require evaluating ranges or performing calculations on the expression being tested,
if
statements can handle such scenarios more naturally.
Use "switch-case" (switch) when:
-
You Need to Compare a Single Expression to Multiple Values: If you have a single expression that needs to be compared to several distinct values,
switch-case
is a more concise and efficient choice.
switch (day) {
case 1:
// Code for Sunday
break;
case 2:
// Code for Monday
break;
// Additional cases...
default:
// Default code
break;
}
-
You Have a Limited Number of Options:
switch
statements are especially useful when you have a small and fixed set of options to consider. It enhances code readability and maintainability when dealing with enumerated or predefined values.
-
You Want to Optimize for Efficiency:
switch
statements are often more efficient than long chains of if-else if
statements, particularly when there are many possible cases to evaluate.
-
Values Are Integral or Enumerated:
switch
statements work well with integral types like integers and characters, as well as enumerated types.
-
In summary, choose the appropriate control structure based on the complexity of your conditions, the number of distinct cases, and the data types involved. Both "if statements" and "switch-case" have their strengths, and selecting the right one will result in more maintainable and readable code.