On this page, you will learn about the switch-case statement and what the types of switch-case statements are in C programming. You will get a brief description of single switch statements and nested switch statements. In addition to this, you will get information about how to write single switch statements and nested switch statements. Further, you will get an example of single switch statements and nested switch statements.
Switch case statement is a control flow statement in C programming that allows the program to execute different actions based on the value of an expression or variable. It is similar to a series of if-else statements, but provides a more concise and efficient way to compare multiple values.
The syntax for a switch case statement in C is as follows:
switch(expression) {
case value1:
// code to be executed if expression matches value1
break;
case value2:
// code to be executed if expression matches value2
break;
...
default:
// code to be executed if expression does not match any case
}
The expression is evaluated once and then compared with each of the case values. If there is a match, the code under that case statement is executed until the break statement is encountered. If there is no match, the code under the default statement is executed.
The switch case statement is often used in situations where there are many possible values for a variable, and each value requires a different action to be taken. This includes things like menu options in a program, days of the week, or different types of input data.
*** One important thing to note about switch case statements is that the expression being evaluated must be an integer, character, or enumeration type. It cannot be a floating-point number or string.
There are two types of switch-case statements in C: the single-switch statement and the nested switch statement.
#include<stdio.h>
int main() {
int num = 3;
switch(num) {
case 1:
printf("Number is 1");
break;
case 2:
printf("Number is 2");
break;
case 3:
printf("Number is 3");
break;
default:
printf("Number is not 1, 2, or 3");
break;
}
return 0;
}
Output:
Number is 3
switch (expression1) {
case 1:
switch (expression2) {
case 1:
// code to execute when expression1 is 1 and expression2 is 1
break;
case 2:
// code to execute when expression1 is 1 and expression2 is 2
break;
}
break;
case 2:
// code to execute when expression1 is 2
break;
}
In the syntax, the outer switch statement evaluates expression1, and if it matches case 1 or 2, the corresponding block of code is executed. If it matches case 1, the inner switch statement is evaluated to determine which block of code to execute based on the value of expression2.
#include <stdio.h>
int main() {
int x = 1;
int y = 2;
int operation = 1;
int result;
switch (x) {
case 1:
switch (y) {
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
default:
printf("Invalid value of y");
return 0;
}
break;
case 2:
switch (y) {
case 1:
result = x * y;
break;
case 2:
result = x / y;
break;
default:
printf("Invalid value of y");
return 0;
}
break;
default:
printf("Invalid value of x");
return 0;
}
printf("The result is: %d", result);
return 0;
}
Output:
The result is: -1
Finally, when using switch case statements in C, there are a few keynotes to keep in mind. First, it's important to note that duplicate case values are not allowed. Additionally, the default statement is optional. If a switch case statement doesn't have a default statement, it will run without any issues.
To terminate a statement sequence within the switch case block, the break statement is used. When the compiler encounters a break statement, it terminates the switch statement and jumps to the next line following the switch.
Nested switch statements are allowed, which means you can have a switch statement inside another switch. However, it's generally recommended to avoid nested switch statements because they can make a program more complex and less readable.