C++ Switch Statement
In this tutorial, we will learn about switch statement and its working in C++ programming with the help of some examples.
Switch Statement
Switch case statements are controlles statement that is regarded as a substitute for if-else statements. Switch statements are used for long if-else 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 Case Statement
The switch
statement allows us to execute a block of code among many alternatives.
switch (expression) {
case 1:
// code to be executed if the expression is equal to case 1
// break statement execute
break;
case 2:
// code to be executed if the expression is equal to case 2
// break statement execute
break;
.
.
default:
// code to be executed if the expression doesn't match any case
}
You need to keep things in mind when using a Switch Statement:
- The expression provided in the switch must be a constant value otherwise it is invalid.
- 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 work without any problem.
- The
break
statement is used to take control out of the loop otherwise all the cases before a break would be executed. - 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 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 though to subsequent cases until a break is reached.
- The switch statement can also be nested, which means you have switch statements inside another switch.
Flowchart of switch Statement
How does the switch statement work?
The expression
is evaluated once and compared with the values of each case
label.
- The value of this expression is then compared with 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 the switch statement terminates.
- 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 case the value is not equal, the code after
default:
is executed.
Note: We can do the same thing with the
if...else..if
ladder. However, the syntax of the
switch
statement is cleaner and much easier to read and write.
Example 1: C++ Program to understand the switch Statement
// a simple program to understand switch Statement
#include <iostream>
using namespace std;
int main() {
int a = 1;
switch (a) {
case 1:
cout << n1 << "My Choice is 1";
break;
case 2:
cout << n1 << "My Choice is 2";
break;
case 3:
cout << n1 << "My Choice is 3";
break;
default:
// Variable "a" doesn't match any case
cout << "Invalid! The Choice is not correct";
break;
}
return 0;
}
Output
My Choice is 1
Example 2: C++ Create a Calculator using the switch Statement
// Program to build a simple calculator using switch Statement
#include <iostream>
using namespace std;
int main() {
char operator;
float n1, n2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> n1 >> n2;
switch (operator) {
case '+':
cout << n1 << " + " << n2 << " = " << n1 + n2;
break;
case '-':
cout << n1 << " - " << n2 << " = " << n1 - n2;
break;
case '*':
cout << n1 << " * " << n2 << " = " << n1 * n2;
break;
case '/':
cout << n1 << " / " << n2 << " = " << n1 / n2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Invalid! The operator is not correct";
break;
}
return 0;
}
Output
Enter an operator (+, -, *, /): + Enter two numbers: 5 3 5 + 3 = 8
Output 2
Enter an operator (+, -, *, /): - Enter two numbers: 5 3 5 - 2 = 3
Output 3
Enter an operator (+, -, *, /): * Enter two numbers: 5 2 5 * 2 = 10
Output 4
Enter an operator (+, -, *, /): / Enter two numbers: 6 2 6 / 2 = 3
Output 5
Enter an operator (+, -, *, /): ? Enter two numbers: 5 2 Error! The operator is not correct.
In the above program, we are using the switch...case
statement
to perform addition, subtraction, multiplication, and division.
How This Program Works
- We first prompt the user to enter the desired operator. This input is
then stored in the
char
variable named oper. - We then prompt the user to enter two numbers, which are stored in the float variables n1 and n2.
- The
switch
statement is then used to check the operator entered by the user:- If the user enters
+
, addition is performed on the numbers. - If the user enters
-
, subtraction is performed on the numbers. - If the user enters
*
, multiplication is performed on the numbers. - If the user enters
/
, division is performed on the numbers. - If the user enters any other character, the default code is printed.
- If the user enters
Notice that the break
statement is used inside each case
block. This terminates the switch
statement.
If the break
statement is not used, all cases after the correct case
are executed.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of Switch Statement in C++.
Keep Learning : )
In the next tutorial, you'll learn about C++ goto Statement
.