Switch Statement
Switch statement is another selection statement that you can use. The switch statement allows you to match multiple values with an expression. For each option, the behaviour could be different.
The following If - Else Statement could be rewritten using a switch statement.
#include <iostream>
using namespace std;
int main()
{
char opt;
cout << "What is your marital status? \n"
<< "m-married, s-single, d-divorced, w-widowed \n"
<< ":";
if (opt == 'm')
cout << "you entered married." << endl;
else if (opt == 's')
cout << "you entered single." << endl;
else if (opt == 'd')
cout << "you entered divorced." << endl;
else if (opt == 'w')
cout << "you entered widowed." << endl;
else
cout << "invalid option." << endl;
return 0;
}
The above if-else statement could be written as a switch Statement shown below.
#include <iostream>
using namespace std;
int main()
{
char opt;
cout << "What is your marital status? \n"
<< "m-married, s-single, d-divorced, w-widowed \n"
<< ":";
cin >> opt;
switch (opt)
{
case 'm':
cout << "you entered married." << endl;
break;
case 's':
cout << "you entered single." << endl;
break;
case 'd':
cout << "you entered divorced." << endl;
break;
case 'w':
cout << "you entered widowed." << endl;
break;
default:
cout << "Invalid option." << endl;
break;
}
return 0;
}
Here is another example of switch statement.
#include <iostream>
using namespace std;
int main()
{
char opt;
double num1, num2, result;
cout << "Enter the first number :";
cin >> num1;
cout << "Enter the second number :";
cin >> num2;
cout << "Enter the operator \n"
<< "a-add, s-substract, m-multiplication, d-division :";
cin >> opt;
switch (opt)
{
case 'a':
result = num1 + num2;
break;
case 's':
result = num1 - num2;
break;
case 'm':
result = num1 * num2;
break;
case 'd':
result = num1 / num2;
break;
default:
cout << "invalid operator";
exit(1);
}
cout << "The answer is : " << result;
return 0;
}
Here is another example:
#include <iostream>
using namespace std;
int main()
{
int opt;
cout << "Main Menu\n"
<< "=========\n"
<< "1. Calculator\n"
<< "2. Temp Converter\n"
<< "3. Exit program\n"
<< "Enter your selection (1,2,3): ";
cin >> opt;
switch (opt)
{
case 1:
cout << "You selected option 1 - Calculator.";
break;
case 2:
cout << "you selected option 2 - Temp Converter.";
break;
case 3:
cout << "You selected option 3 - the program will end now";
break;
default:
cout << "Invalid input. Please restart program.";
}
return 0;
}
Another example
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int op;
double fnum, snum;
cout << "Calculator\n"
<< "=========\n"
<< "Enter the first number : ";
cin >> fnum;
cout << "Enter the second number : ";
cin >> snum;
cout << "Select the operator: \n"
<< "1 - addition\n"
<< "2 - substraction \n"
<< "3 - multiplication \n"
<< "4 - division \n"
<< "5 - power of \n"
<< "Enter your option: ";
cin >> op;
switch (op)
{
case 1:
cout << "Addition: " << fnum << " + " << snum << " = " << fnum + snum;
break;
case 2:
cout << "Substraction: " << fnum << " - " << snum << " = " << fnum - snum;
break;
case 3:
cout << "Multiplication: " << fnum << " * " << snum << " = " << fnum * snum;
break;
case 4:
cout << "Division: " << fnum << " / " << snum << " = " << fnum / snum;
break;
case 5:
cout << "Power of: " << fnum << " raised to the power " << snum << " is " << pow(fnum, snum);
break;
default:
cout << "Invalid option " ;
break;
}
return 0;
}
Comments
Post a Comment