Skip to main content

Switch Statement

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

Popular posts from this blog

A Comprehensive Evaluation of the Internal Consulting Process: Steps and Considerations

Introduction Internal consulting has emerged as a critical function within organizations, offering in-house expertise to solve complex business problems and drive change. It closely mirrors external consulting in methodology but is differentiated by the consultant's intimate knowledge of the organization and a vested interest in its long-term success. This article aims to evaluate the key steps involved in the internal consulting process, offering insights into each phase's significance and challenges. Steps in the Internal Consulting Process The internal consulting process can generally be segmented into five distinct stages: Initial Assessment, Data Collection and Analysis, Solution Development, Implementation, and Evaluation. Below is an evaluation of each step: Step 1: Initial Assessment Objective: To understand the problem or opportunity area and define the scope of the project. Significance: A well-defined scope ensures that the consulting project stays focused and manage

Excel PMT Function

PMT function is very useful for calculating monthly payment required to payback a loan or mortgage at a fixed rate. This function require a minimum of three inputs, periodic rate, number of periods, present value or the loan amount. Here is a simple example. Home Loan: 350,000.00 Interest rate: 4.5% Number of years to repay the loan: 25 Note: To calculate monthly payment, we need to find the monthly rate and number of months as shown above. Then it is simply a matter of substituting the values into the payment function, as shown in the formula view below.

CUMIPMT and CUMPRINC function

CUMIPMT Cumulative interest payment function allows you to calculate the interest paid for a loan or from an investment from period A to period B. When getting a loan, CUMIPMT function can be used to calculate the total amount of interest paid in the first five months or from period 12 to period 20. A period can be a month, a week or two week. Loan Amount : 350,000.00 APR: 4.5% Down payment: 0.00 Years: 25 Payment per year: 12 From the above data, we can calculate the following: No of Period: 25 × 12 = 300 Periodic Rate: 4.5/12 = 0.375% Here is how you will substitute these values into the function. = CUMIPMT (periodic rate, No of period, vehicle price, start period, end period,  ) = CUMIPMT (0.375, 300, 350000, 1, 5, 0) In an excel worksheet, we use cell address instead of actual values as shown below: Here is the formula view of the worksheet: CUMPRINC Another related function is CUMPRINC. CUMPRINC function is used to calculate cumul