switch, case

Tests a condition against any number of case conditions.

 

Break must be used to terminate the switch, or the next statement will be executed.

 

A default case is utilised for when no condition is met.

 

#include <iostream>
using namespace std ;

int main () {

	int myVar;

	cout << "Please enter a value of 1 or 2:" << endl ;
	cin >> myVar ;

	switch (myVar)
	{
		case 1:
			cout << "You entered a value of 1" << endl ;
			break;
		case 2:
			{
				cout << "You entered a value of 2" << endl ;
				break;
			}
		default:
			cout << "Invalid choice" << endl ;
			break;
	}
	return 0;
}

Leave a Reply