In C programming, the switch statement is another way to make decisions based on the value of a variable.
It's often used when we have a single variable and want to execute different code blocks based on its value.
Multi-Way Decision: The switch statement allows us to choose from multiple statements based on the value of a single expression.
Constant Expressions: The expression provided to the switch statement case must be a constant value, such as integers, characters, or enumerated types.
Case Labels: Each case label defined within the switch statement specifies a constant value to match against the expression.
Default Case: The default label condition serves as a fallback option if none of the case labels match the value of the expression.
Efficient Execution: switch statements are often more efficient than if-else chains, especially when dealing with multiple exclusive cases.
Cleaner Code: switch statements provide cleaner and more readable code, especially when dealing with multiple options.
The syntax of the switch statement is as follows:
switch (expression) { case constant1: // code block break; case constant2: // code block break; ... case constantN: // code block break; default: // code block }
The "expression" is typically a variable whose value we want to check against various constants.
Inside the "switch" block, there are multiple "case" labels, each representing a possible value of the "expression".
When the "switch" statement is executed, the value of the "expression" is compared to each "case" constant.
If a match is found, the corresponding code block is executed.
The "break" statement is used to exit the "switch" block. Without "break", the control will fall through to the next "case", executing its "code" block as well.
The "default" case is optional. It's executed when none of the "case" constants match the value of the "expression".
#include <stdio.h> int main() { int choice = 2; switch (choice) { case 1: printf("You choose option 1\n"); break; case 2: printf("You choose option 2\n"); break; case 3: printf("You choose option 3\n"); break; default: printf("Invalid choice\n"); } return 0; }
in the above example, we have defined `choice=2`, when the code executes it will enter in switch condition, and it will look for a matching case in our case `case 2` is matching, so it will enter into the case 2 condition and print `You choose option 2`.
if no case matches then it will enter in default case statement and print `Invalid choice`.
printing Day Name based on the user input value.
#include <stdio.h> int main() { int day; printf("Enter a 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\n"); } return 0; }
if the choice is 2, it will print "You choose option 2". If the choice is 1 or 3, it will print the corresponding message.
If the choice is other than 1-7, it will print "Invalid choice" because of the default case.