Table of Contents
Switch case statement in C++ language
In this tutorial, we will discuss the switch case statement in C++ language.
Switch statements
Essential points about this statements
- The switch statement is one of the java statement allows a unique variable to be checked for equality against a list of the case value of the same type variable(int, String, char can use anyone )
- Switch case similar if-else statement functioning based Boolean expression.
- This statement choose and executes one from multiple conditions, that multiple conditions are called cases
- Any number of the case may use within a switch and every case statement contain a value to compared with switch value. case and case value separated by a colon(case: value)
- End of the switch can have a default statement the default statement can be used for performing a task when becoming false
Syntax
switch(expression){ case constant- expression 1: ststements; // code to be executed if expression is equal to constant 1 break; case constant- expression 2: ststements;// code to be executed if expression is equal to constant 2 break; //any number of case statement default; statements; //code to be executed if n doesn't match any constant }
The switch statement checks the expression. If it is found to match the case statement, control of the program passes to the block of codes associated with the case statements. The break statement is used to stop the control and prevent from running to the next case.
Flow diagram
How to work this statements
- The switch statement is evaluated once
- The value of the switch expression is compared with the value of each case.
- When there is a match of the particular case, the associated block of statements is executed.
- When there is not match any cases, the default statement is executed.
Program 2
#include <iostream> #include <conio.h> using namespace std; int main() { char ch; int num1,num2; cout << "Enter an operator as you wish!" << endl; cin>>ch; cout << "Enter two numbers for your calculation!" << endl; cin>>num1>>num2; switch(ch) { case '+': cout<<"Addition of "<<num1<<" and "<<num2<<": "<<num1+num2; break; case '-': cout<<"difference of "<<num1<<" and "<<num2<<": "<<num1-num2; break; case '*': cout<<"product of "<<num1<<" and "<<num2<<": "<<num1*num2; break; case '/': cout<<"division of "<<num1<<" and "<<num2<<": "<<num1/num2; break; } getch(); return 0; }
The above code is executed it produces the following result
Case 1
Enter an operator as you wish!
+
Enter two numbers for your calculation!
12
23
Addition of 12 and 23: 35
Case 2
Enter an operator as you wish!
–
Enter two numbers for your calculation!
45
23
the difference of 45 and 23: 22
Case 3
Enter an operator as you wish!
*
Enter two numbers for your calculation!
12
13
the product of 12 and 13: 156
Case 4
Enter an operator as you wish!
/
Enter two numbers for your calculation!
200
20
division of 200 and 20 : 10
The operator is entered by the user as they wish and it is stored in the ch variable.
Then two numbers are entered by the user and stored in variables num1 and num2 respectively
Next, the control of the program moves to the relevant case and displays the output
Finally, the break statement ends the switch statements
Program 3
#include <iostream> #include <conio.h> using namespace std; int main() { int score; cout << "Enter your score (1 to 100):" << endl; cin>>score; switch(score/10){ case 10: case 9: case 8: cout<<"you got merit pass"; break; case 7: case 6: cout<<"you have passed with first division"; break; case 5: cout<<"you have passed with second division"; break; case 4: case 3: cout<<"you have passed with third division"; break; default: cout<<"Sorry, you try again once"; } getch(); return 0; }
The above code is executed it produces the following result
Enter your score ( 1 to 100): 56 you have passed with second division