Last Updated on August 9, 2023 by Mayank Dham
The switch statement in C is a conditional branching statement that evaluates an expression, and branches to the matching case label within the switch statement block. It is used to handle multiple selections of code, with the expression being tested against a series of constant values (case labels) to determine which block of code to execute.
What is the Switch Statement in C?
The switch statement in C is a control flow mechanism that allows a program to make decisions based on the value of a variable. It is an efficient and readable alternative to a series of if-else statements, and it consists of an expression that is evaluated and compared to a series of case values.
Convert If-Else Code into Switch Statements in C
Let’s see how to convert the if-else code into the switch case statement.
If-Else Code:
int num=1;
if(num>0){
printf("%s","The number is positive");
}
else if(num<0){
printf("%s","The number is negative");
}
else{
printf("%s","The number is zero");
}
The Switch Case Statement:
int num=1;
switch(num){
case 1:
printf("%s","The number is positive");
break;
case -1:
printf("%s","The number is negative");
break;
default:
printf("The number is zero");
}
In the switch case statement, If a case value matches the expression, the corresponding statements are executed, and the program continues until it encounters a break statement or reaches the end of the switch statement. If no case values match the expression, the default statement is executed, if provided. The switch statement is often used for making decisions based on well-defined, large sets of values and provides a more concise and maintainable solution compared to complex nested if-else statements.
Syntax of the Switch Case Statement in C:
Let’s see what is the syntax of the switch case statement in c:
switch (expression) {
case constant-expression:
statement(s);
break;
case constant-expression:
statement(s);
break;
...
default:
statement(s);
}
Let’s understand the above syntax of the switch case statement in c:
- The switch keyword followed by an expression in parentheses. This expression is evaluated, and its value is compared to the constant expressions in each of the case labels.
- The case keyword followed by a constant expression and a colon. If the value of the switch expression matches the constant expression in a case label, the statements following that case label will be executed.
- One or more statements that follow a case label.
- The optional break statement, which terminates the switch statement and transfers control to the statement following the switch statement.
- The optional default keyword followed by a colon and one or more statements. The statements following the default label are executed if the value of the switch expression does not match any of the constant expressions in the case labels.
Flowchart of the Switch Statement in C:
- The expression inside the parentheses following the switch keyword is evaluated.
- The value of the expression is compared to the constant expressions in each of the case labels.
- If a match is found, the statements following that case label are executed.
- If a break statement is encountered, the switch statement is terminated, and control is transferred to the statement following the switch statement.
- If no break statement is encountered, control flows through to the next case label, even if a match has been found.
- If no match is found and the default label is present, the statements following the default label are executed.
- The switch statement is complete, and control is transferred to the statement following the switch statement.
What are the Important Keywords of the Switch Statement:
Given the following keywords of the switch statement in C:
-
Break: To stop the execution of the case block this keyword is used and it also uses to terminate the switch block.
-
Default: If there is no case match in the switch statement then this keyword is used to execute a particular set of statements.
Examples of the Switch Statement in C:
Example 1:
#include <stdio.h> int main() { int choice; printf("Enter a number (1-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 number.\n"); break; } return 0; }
Output:
Enter a number (1-3): 2
You entered 2.
Time Complexity: O(1)
Space Complexity: O(1)
In the above program, the user is prompted to enter a number. The number is then stored in the choice variable. The switch case statement is used to match the value of choice with different cases (1, 2, 3) and execute the corresponding code block. If the value of choice doesn't match any of the cases, the code inside the default case is executed.
Example 2:
#include <stdio.h> int main() { int day; printf("Enter a number for a day of the week (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.\n"); break; } return 0; }
Output:
Enter a number for a day of the week (1-7): 6
Saturday
Time Complexity: O(1)
Space Complexity: O(1)
In the above program, the user is asked to enter a number representing a day of the week. The number is stored in the day variable and used in the switch case statement to match with different cases (1, 2, 3, 4, 5, 6, 7). Based on the matching case, the corresponding day of the week is printed. If the number entered by the user doesn't match any of the cases, the code inside the default case is executed.
Conclusion:
This article explains what a switch statement in C is as well as the syntax of a switch statement. You can also learn about some important switch statement terms and the switch statement flowchart. Finally, the provided example will clarify the use of the switch statement.So make sure to practice the questions provided below and it is very important to understand the switch statement syntax as well.
Frequently Asked Questions (FAQs)
Here are some of the frequently asked questions related to the switch statement.
Q1: What is a switch statement in C?
Answer: A switch statement in C is a control flow construct that evaluates an expression and directs the program's execution to a specific block of code based on the value of that expression.
Q2: What is the alternative name for a switch statement?
Answer: A switch statement is also known as a "switch-case" statement.
Q3: Why should you use switch statements?
Answer: Switch statements offer a more organized and efficient way to handle multiple conditional cases compared to using multiple if-else statements. They enhance code readability and can improve performance, particularly when dealing with numerous possible outcomes.
Q4: What is the example of a switch statement in C?
Answer: Here's an example of a switch statement used to determine the day of the week based on an integer input:
#include
int main() {
int day = 4;
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;
default:
printf("Weekend\n");
}
return 0;
}
In this example, the program evaluates the value of day and prints the corresponding day of the week using the switch statement.