Examples of switch Statement in C++
Example 1: C++ Program to make a Simple Calculator using the switch
// C Program to create a simpe calculator using switch
// statement
#include <iostream>
#include <stdlib.h>
using namespace std;
// driver code
int main()
{
// switch variable
char choice;
// operands
int x, y;
while (1) {
cout << "Enter the Operator (+,-,*,/)\nEnter x to "
"exit\n";
cin >> choice;
// for exit
if (choice == 'x') {
exit(0);
}
cout << "Enter the two numbers: ";
cin >> x >> y;
// switch case with operation for each operator
switch (choice) {
case '+':
cout << x << " + " << y << " = " << x + y
<< endl;
break;
case '-':
cout << x << " - " << y << " = " << x - y
<< endl;
break;
case '*':
cout << x << " * " << y << " = " << x * y
<< endl;
break;
case '/':
cout << x << " / " << y << " = " << x / y
<< endl;
break;
default:
printf("Invalid Operator Input\n");
}
}
return 0;
}
Output
Enter the operator (+, -, *, /)
Enter x to exit
+
Enter the two numbers: 100 + 200
100 + 200 = 300
Example 2: C++ Program to print the day using switch case
// C++ program to returns day based on the numeric value.
#include <iostream>
using namespace std;
class Day {
private:
int day = 3;
public:
void set_data()
{
cout << "Enter no of day you want to display: ";
cin >> day;
}
void display_day()
{
switch (day) {
case 1:
cout << "MONDAY";
break;
case 2:
cout << "TUESDAY";
break;
case 3:
cout << "WEDNESDAY";
break;
case 4:
cout << "THURSDAY";
break;
case 5:
cout << "FRIDAY";
break;
case 6:
cout << "SATURDAY";
break;
case 7:
cout << "SUNDAY";
break;
default:
cout << "INVALID INPUT";
break;
}
}
};
main()
{
Day d1;
d1.display_day();
return 0;
}
Output
WEDNESDAY
Example 3: C++ Program to print different choices using enum values and class
// C++ Program to print different choices using enum values
// and class
#include <iostream>
#include <map>
#include <string>
using namespace std;
// Define an enum class for the choices
enum class Choice { A, B, C };
// Create a map to associate strings with the enum values
map<std::string, Choice> stringToEnumMap
= { { "A", Choice::A },
{ "B", Choice::B },
{ "C", Choice::C } };
int main()
{
// The input string
string x = "A";
// Convert the string to the corresponding enum using
// the map
Choice choice = stringToEnumMap[x];
// Use a switch statement on the enum
switch (choice) {
case Choice::A:
cout << "Choice is A";
break;
case Choice::B:
cout << "Choice is B";
break;
case Choice::C:
cout << "Choice is C";
break;
default:
cout << "Choice other than A, B and C";
break;
}
return 0;
}
/* The enum class can be used for choice sets
from different projects or programs,
hence improving modularity*/
Output
Choice is A
Switch Statement in C++
The C++ Switch case statement evaluates a given expression and based on the evaluated value(matching a certain condition), it executes the statements associated with it. It is an alternative to the long if-else-if ladder which provides an easy way to dispatch execution to different parts of code based on the value of the expression.