Switch case statements are a substitute for long if statements that compare a variable to several integral values
- The switch statement is a multiway branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
- Switch is a control statement that allows a value to change control of execution.
Syntax:
switch (n)
{
case 1: // code to be executed if n = 1;
break;
case 2: // code to be executed if n = 2;
break;
default: // code to be executed if n doesn't match any cases
}
Important Points about Switch Case Statements:
- The expression provided in the switch should result in a constant value otherwise it would not be valid.
Valid expressions for switch:// Constant expressions allowed switch(1+2+23) switch(1*2+3%4) // Variable expression are allowed provided // they are assigned with fixed values switch(a*b+c*d) switch(a+b+c)
- Duplicate case values are not allowed.
- The default statement is optional.Even if the switch case statement do not have a default statement,
it would run without any problem. - The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- The break statement is optional. If omitted, execution will continue on into the next case. The flow of control will fall through to subsequent cases until a break is reached.
- Nesting of switch statements are allowed, which means you can have switch statements inside another switch. However nested switch statements should be avoided as it makes program more complex and less readable.
Flowchart

Example:
C
// Following is a simple C program // to demonstrate syntax of switch. #include <stdio.h> int main() { int x = 2; switch (x) { case 1: printf("Choice is 1"); break; case 2: printf("Choice is 2"); break; case 3: printf("Choice is 3"); break; default: printf("Choice other than 1, 2 and 3"); break; } return 0; } |
C++
// Following is a simple C++ program // to demonstrate syntax of switch. include <iostream> using namespace std; int main() { int x = 2; switch (x) { case 1: cout << "Choice is 1"; break; case 2: cout << "Choice is 2"; break; case 3: cout << "Choice is 3"; break; default: cout << "Choice other than 1, 2 and 3"; break; } return 0; } |
Output:
Choice is 2
Related article:
- Interesting facts about Switch Case in C
- What should be data type of case labels of switch statement in C?
- Print individual digits as words without using if or switch
This article is contributed by Somesh Awasthi. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Data type of case labels of switch statement in C++?
- Interesting facts about switch statement in C
- Nested switch statement in C++
- Switch Statement in Java
- Print individual digits as words without using if or switch
- Decision Making in Java (if, if-else, switch, break, continue, jump)
- Output of C programs | Set 30 (Switch Case)
- String in Switch Case in Java
- switch vs if else
- Using range in switch case in C/C++
- Menu-Driven program using Switch-case in C
- C++17 new feature : If Else and Switch Statements with initializers
- Nested switch case
- Print "Even" or "Odd" without using conditional statement
- return statement vs exit() in main()
- Print the pattern by using one loop | Set 2 (Using Continue Statement)
- Implementing ternary operator without any conditional statement
- Break Statement in C/C++
- Continue Statement in C/C++
- goto statement in C/C++
Improved By : arvindnikesh

