Control Flow: Conditional Statements (if, else, switch)
Using `if`, `else`, and `switch` statements to control the flow of execution based on conditions.
C Programming: Conditional Statements - Switch Statement
Control Flow: Conditional Statements (switch)
The switch
statement in C is a multi-way decision statement. It provides a clean and efficient way to execute different blocks of code based on the value of a single expression. It is particularly useful when you need to compare a variable against several constant values.
The basic syntax of a switch
statement is:
switch (expression) {
case constant1:
// Code to execute if expression == constant1
break;
case constant2:
// Code to execute if expression == constant2
break;
case constant3:
// Code to execute if expression == constant3
break;
default:
// Code to execute if none of the cases match
}
Here's how it works:
- The
expression
inside the parentheses is evaluated. This expression must evaluate to an integer, character, or enumeration type. - The result of the expression is then compared with each
case
label. - If a
case
label matches the value of the expression, the code block associated with thatcase
is executed. - If no
case
matches the expression, the code block associated with thedefault
label (if present) is executed. - The
break
statement is crucial for controlling the flow of execution within theswitch
statement.
The Importance of break
and default
Keywords
break
Keyword
The break
keyword is essential in a switch
statement. When a case
matches and its code block is executed, the break
statement terminates the execution of the switch
statement. Without a break
statement, execution will "fall through" to the next case
, regardless of whether its constant matches the expression. This is often *not* the desired behavior. This "fall-through" can be useful in specific scenarios, but it is generally best practice to explicitly include break
statements to avoid unintended consequences.
Example without break
(demonstrating fall-through):
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
printf("Case 1 executed\n");
case 2:
printf("Case 2 executed\n");
case 3:
printf("Case 3 executed\n");
default:
printf("Default case executed\n");
}
return 0;
}
Output:
Case 2 executed
Case 3 executed
Default case executed
As you can see, because there are no break
statements, execution falls through from case 2
to case 3
and then to the default
case.
Example with break
(correct usage):
#include <stdio.h>
int main() {
int x = 2;
switch (x) {
case 1:
printf("Case 1 executed\n");
break;
case 2:
printf("Case 2 executed\n");
break;
case 3:
printf("Case 3 executed\n");
break;
default:
printf("Default case executed\n");
break;
}
return 0;
}
Output:
Case 2 executed
Now, only case 2
is executed, as intended.
default
Keyword
The default
keyword provides a fallback case when none of the case
labels match the expression. It is optional, but highly recommended. It allows you to handle situations where the expression has an unexpected value. It's good practice to include a default
case, even if you believe all possible values are covered by the other cases, to catch unforeseen errors or changes in the input.
If the default
case is present, it is typically placed at the end of the switch
statement, though this is not strictly enforced by the C compiler. However, maintaining this convention improves code readability.
Example demonstrating default
:
#include <stdio.h>
int main() {
int x = 5;
switch (x) {
case 1:
printf("Case 1 executed\n");
break;
case 2:
printf("Case 2 executed\n");
break;
case 3:
printf("Case 3 executed\n");
break;
default:
printf("Default case executed: x is not 1, 2, or 3\n");
break;
}
return 0;
}
Output:
Default case executed: x is not 1, 2, or 3
In this example, since x
is 5 and doesn't match any of the case
labels, the default
case is executed.