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:
In conclusion, this article helps to understand what is switch statement in c and also understand the syntax of the switch statement. In addition, you can learn about some important terms of the switch statement and the flow chart of the switch statement. In the end, the given example will make the use of the switch statement more clear.
FAQs Related to Switch Statement
1. Which data types can be used in the switch statement in c?
The main 4 data types that can be used in the switch statement in c are int, char, short and long. However, float data can not be used in the switch statement.
2. Can we use multiple case statements to execute the same block?
Yes, we can use multiple case statements to execute the same block, the example is given below.
switch(ch){
case 'A':
case 'B':
case 'C':
printf("A, B or C is entered");
break;
}
3. Can I use expressions as case labels in a switch case statement?
No, you cannot use expressions as case labels in a switch case statement. Only constant integer expressions can be used as case labels.
4. Can we write the switch statement without using default?
Yes, we can write the switch statement in c without using the default. The example is given below.
#include <stdio.h> int main() { int num; num=4; switch(num){ case 1: printf("Number is 1"); break; case 2: printf("Number is 2"); break; case 3: printf("Number is 3"); break; } return 0; }
In the above example, the program will run without any error but it will not print anything as we have not given the default block.
5. Can I use the return statement inside a switch case statement?
Yes, we can use the return statement inside the switch case statement. The example is given below.
#include <stdio.h> int calculate(char operator, int x, int y) { switch (operator) { case '+': return x + y; case '-': return x - y; case '*': return x * y; case '/': return x / y; default: return 0; } } int main() { int num1, num2; char operator; printf("Enter two numbers and an operator (+, -, *, /): "); scanf("%d %d %c", &num1, &num2, &operator); int result = calculate(operator, num1, num2); printf("Result: %d\n", result); return 0; }
Output:
Enter two numbers and an operator (+, -, *, /): 2 3 *
Result: 6