Updated March 16, 2023
What is a Switch Statement in C++?
Switch case statements are controlled statement that is regarded as a substitute for if-else statements. A switch statement in c++ is a multiway branch statement that provides a way to organize the flow of execution to parts of code based on the value of the expression. In a very basic term, a switch statement evaluates an expression, tests it and compares it against the several cases written in the code. As soon as a match with any case is found, the control will enter that case and start executing the statements written in that case until a break statement has been found. As soon as a break statement appears, the switch statement terminates, and the program control exits the switch.
It might sometimes happen that no case matches up with the value of the expression. For such cases, we mention a default case that will always execute in case if no match is found. The cases in a block of a switch statement are represented by different numbers or string, which is known as an identifier. The value of the expression or the value provided by the user is compared with these cases until the match is found.
Syntax of C++ Switch Statement
The syntax for switch statement in C++ programming language is given below-
switch( expression )
{
case value1:
//Block of code;
break;
case value2:
//Block of code;
break;
case valueN:
//Block of code
break;
default:
//Block of code
break;
You need to keep the following things in mind when using a Switch Statement :
- Case labels can be an integer or a character, and they should be unique
- Case labels always end with a semicolon.
- Even though a default case label is not mandatory, it can at most be one if defined.
- You need a break statement to take the control out of the loop; otherwise, all the cases before a break would be executed.
- The default case label does not have any specific position.
- The switch statement can also be nested.
Flowchart of Switch Statement
The flowchart is as follows:
How does Switch Statement work in C++?
Let us understand the flow of control depicted in the flowchart above in order to gain a better understanding of the flow of execution.
An expression is passed with the switch statement, which is equal to one of the values of the cases. In case the value is not equal, the default case is executed. The value of this expression is then compared with the case identifier or the first case. If the first case matches, then the block of code associated with the first case is executed. Once the break is encountered, the execution stops, and you will exit the switch statement. However, if the case does not match, the execution flows to the next case. If this case matches, then the second code block executes; otherwise, the flow checks the next case in a similar way. Finally, if no case matches, then the default code block is executed.
Examples of Switch Statement in C++
Lets us see some of the examples of the switch statement in C++.
Example #1
This example will give more clarity about the use of switch statements.
Code:
#include <iostream>
using namespace std;
int main () {
char grade_report = 'D';
cout << "Your performance is: " << endl;
switch(grade_report) {
case 'A' :
cout << "Outstanding Result!\n" << endl;
break;
case 'B' :
cout << "Excellent Result!\n" << endl;
break;
case 'C' :
cout << "Good Result\n" << endl;
break;
case 'D' :
cout << "Satisfying Result\n" << endl;
break;
case 'F' :
cout << "Poor Result\n" << endl;
break;
default :
cout << "You did not appear for exam\n" << endl;
}
return 0;
}
Output:
Example #2
This example depicts the use of the break statement in a switch. If the break statement is not specified after the case, the execution flow will continue until it encounters the break statement.
Code:
#include <iostream>
using namespace std;
int main() {
int range_of_number=50;
switch (range_of_number) {
case 10:
case 20:
case 30:
cout << "The number is 10 or 20 or 30 " << endl;
break;
case 50:
case 55:cout << "This case also executes because there is no break " << endl;
cout << "\n" << endl;
case 60:
cout << "The number is either 40 or 50 or 60" << endl;
break;
default:
cout << "The number is greater than 60" << endl; }}
Output:
Example #3
Code:
#include <iostream>
using namespace std;
int main()
{
int x = 10, y = 5;
switch(x==y && x+y<10)
{
case 1:
cout << "hi" << endl;
break;
case 0:
cout << "bye" << endl;
break;
default:
cout << " Hello bye " << endl;
}}
Output:
Example #4
Nested Switch Statement
Code:
#include <iostream>
using namespace std;
int main() {
int ID = 300;
int password = 1000;
cout << "Enter Your ID:\n " << endl;
scanf("%d", & ID);
switch (ID) {
case 300:
cout << "Enter your password:\n " << endl;
scanf("%d", & password);
switch (password) {
case 1000:
cout << "Welcome to the portal\n" << endl;
break;
default:
cout << "Enter correct password" << endl;
break;
}
break;
default:
cout << "Enter correct ID" << endl;
break;
}}
Output:
This will depend upon the values entered by the user.
Output 1:
Output 2:
Output 3:
Conclusion
You must know that the basic flow and functionality of the switch statement remains the same in all the programming languages. The difference can be seen only in the general syntax based on the programming language being used.
Recommended Articles
This has been a guide to the Switch Statement in C++. Here we discuss the Concept, Syntax, Working, and Examples of Switch Statement in C++. You can also go through our other suggested articles to learn more –