C++ Switch Case Statement with Program EXAMPLES
What is a switch?
The switch statement helps in testing the equality of a variable against a set of values. Each value under comparison is known as a case.
See the switch as a multiway branch statement. You can shift the execution of the program to various parts based on the value of the expression.
When to use a switch?
The switch is similar to the if…else…if ladder. However, it generates a cleaner and easy-to-understand code. The switch is also faster compared to the if…else…if ladder. Use the switch statement when you need to compare the value of a variable against a set of other values.
The break Keyword
The break keyword is used inside the switch statement. It prevents the code from running into the next case. It terminates a statement sequence.
When the C++ compiler encounters a break keyword, execution of the switch terminates, and control jumps to the line that comes after the switch statement. The use of a break statement in a switch is optional. If not used, execution continues to the next case.
Syntax
Here is the syntax for switch statement:
switch (variable) { case 1: break; case 2: break; default: }
The above parameters are explained below:
- Variable: This is the variable for which comparison is to be made.
- Case: There are many case statements. Each compares the variable with a different value.
- Break: This keyword prevents execution from continuing to the next case statement.
- Default: This is optional. It states what should be done, the value of the variable did not match any case.
Switch Case Program Example 1
#include<iostream> using namespace std; int main() { int x = 20; switch (x) { case 10: cout<<"X is 10"; break; case 20: cout << "X is 20"; break; case 30: cout << "X is 30"; break; default: cout<<"X is not 10, 20 or 30"; break; } return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added.
- The { marks start of body of the main() function.
- Declaring a variable x and initializing it to 20.
- Using the switch statement and passing the argument x to it. It means that we need to compare the value of variable x to a set of other values.
- The { marks start of the switch body.
- Comparing the value of variable x to a value of 10.
- Statement to be executed if above case is true, that is, if x is 10. The break prevents execution from continuing to the next case.
- Comparing the value of variable x to a value of 20.
- Statement to be executed if above case is true, that is, if x is 20. The break prevents execution from continuing to the next case.
- Comparing the value of variable x to a value of 30.
- Statement to be executed if above case is true, that is, if x is 30. The break prevents execution from continuing to the next case.
- The default helps us state what to be done if the value of variable x is not 10, 20, or 30.
- Statement to be executed if above cases are not true, that is, if x is not 10, 20, or 30.
- End of the body of a switch statement.
- The main() function should return a value if the program runs fine.
- End of the body of the main() function.
Also Check our article on Difference Between C and C++:- Click Here
Switch Case Program Example 2
#include <iostream> using namespace std; int main() { int choice; cout << "Enter 1, 2 or 3: "; cin >> choice; switch (choice) { case 1: cout << "Choice 1"; break; case 2: cout << "Choice 2"; break; case 3: cout << "Choice 3"; break; default: cout << "Not 1, 2 or 3"; break; } }
Output:
Here is a screenshot of the code:
Code Explanation:
- Including the iostream header file in our code. It will allow us to read from and write to the console.
- Including the std namespace so as to use its classes and functions without calling it.
- Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
- Declaring an integer variable named choice.
- Printing some text on the console.
- Prompting the user to enter the value of choice.
- Using the switch statement and passing the argument choice to it. It means that we need to compare the value of variable choice to a set of other values.
- The { marks start of the switch body.
- Comparing the value of variable choice to a value of 1.
- Statement to be executed if the above case is true, that is, if choice is 10. The break prevents execution from continuing to the next case.
- Comparing the value of variable choice to a value of 2.
- Statement to be executed if the above case is true, that is, if choice is 2. The break prevents execution from continuing to the next case.
- Comparing the value of variable choice to a value of 3.
- Statement to be executed if above case is true, that is, if choice is 3. The break prevents execution from continuing to the next case.
- The default helps us state what to be done if the value of variable choice is not 1, 2, or 3.
- Statement to be executed if above cases are not true, that is, if choice is not 1, 2, or 3.
- End of the body of switch statement.
- End of the body of the main() function.
Summary
- The switch statement helps us create a simple if…else…if ladder.
- The switch statement has a clear and simple syntax than if…else…if ladder.
- The switch statement should be used when you need to compare the value of a variable to a set of other values.
- The values are added to case statements.
- The break keywords stop the execution from continuing to the next case.
- An optional default part is used to state action to be taken if no case is matched.