Table of Contents
In this tutorial, we will discuss the switch case statement in C++ language.
Essential points about this statements
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
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
Subtract two numbers using method overriding Program 1
PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…
Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…
Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…
Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…
How to write a program to convert Fahrenheit into Celsius In this article, we will…
This website uses cookies.