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

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

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.

BCG's Brand Advocacy Index

The Boston Consulting Group's (BCG) Brand Advocacy Index (BAI) is a metric developed to help companies measure the degree of customer advocacy for their brands. BAI focuses on the likelihood of customers to recommend a brand to others, which is a powerful indicator of brand strength and customer loyalty. Unlike other customer satisfaction or loyalty metrics, BAI emphasizes the importance of customer referrals and word-of-mouth marketing. BAI is calculated based on a survey where customers are asked about their willingness to recommend a brand to their friends, family, or colleagues. The responses are then used to compute a score, which ranges from -100 to 100. A higher BAI score indicates that a brand has more advocates who are likely to recommend the brand to others, while a lower score suggests that the brand has fewer advocates or even a higher number of detractors. BCG's research has shown that companies with higher BAI scores tend to experience higher growth rates and bett