C++ Switch Case Statement with EXAMPLE

What is a switch?

The switch statement helps in testing the equality of a variable against a set of values. Each value under comparison is known as a case.

See the switch as a multiway branch statement. You can shift the execution of the program to various parts based on the value of the expression.

In this C++ Tutorial, you will Learn:

When to use a switch?

The switch is similar to the if…else…if ladder. However, it generates a cleaner and easy-to-understand code. The switch is also faster compared to the if…else…if ladder. Use the switch statement when you need to compare the value of a variable against a set of other values.

The break Keyword

The break keyword is used inside the switch statement. It prevents the code from running into the next case. It terminates a statement sequence.

When the C++ compiler encounters a break keyword, execution of the switch terminates, and control jumps to the line that comes after the switch statement. The use of a break statement in a switch is optional. If not used, execution continues to the next case.

Syntax

Here is the syntax for switch statement:

switch (variable)
{
    case 1: 
        break;
    case 2: 
        break;
    default: 
}	

The above parameters are explained below:

  • Variable: This is the variable for which comparison is to be made.
  • Case: There are many case statements. Each compares the variable with a different value.
  • Break: This keyword prevents execution from continuing to the next case statement.
  • Default: This is optional. It states what should be done, the value of the variable did not match any case.

Example 1

#include<iostream> 
using namespace std;
int main()
{
	int x = 20;
	switch (x)
	{
	case 10: 
		cout<<"X is 10"; break;

	case 20: 
		cout << "X is 20"; break;

	case 30: 
		cout << "X is 30"; break;

	default: 
		cout<<"X is not 10, 20 or 30"; break;

	}
	return 0;
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added.
  4. The { marks start of body of the main() function.
  5. Declaring a variable x and initializing it to 20.
  6. Using the switch statement and passing the argument x to it. It means that we need to compare the value of variable x to a set of other values.
  7. The { marks start of the switch body.
  8. Comparing the value of variable x to a value of 10.
  9. Statement to be executed if above case is true, that is, if x is 10. The break prevents execution from continuing to the next case.
  10. Comparing the value of variable x to a value of 20.
  11. Statement to be executed if above case is true, that is, if x is 20. The break prevents execution from continuing to the next case.
  12. Comparing the value of variable x to a value of 30.
  13. Statement to be executed if above case is true, that is, if x is 30. The break prevents execution from continuing to the next case.
  14. The default helps us state what to be done if the value of variable x is not 10, 20, or 30.
  15. Statement to be executed if above cases are not true, that is, if x is not 10, 20, or 30.
  16. End of the body of a switch statement.
  17. The main() function should return a value if the program runs fine.
  18. End of the body of the main() function.

Example 2

#include <iostream>  
using namespace std;
int main() {
	int choice;
	cout << "Enter 1, 2 or 3: ";
	cin >> choice;
	switch (choice)
	{
	case 1: 
		cout << "Choice 1"; break;
	case 2: 
		cout << "Choice 2"; break;
	case 3: 
		cout << "Choice 3"; break;
	default: 
		cout << "Not 1, 2 or 3"; break;
	}
}

Output:

Here is a screenshot of the code:

Code Explanation:

  1. Including the iostream header file in our code. It will allow us to read from and write to the console.
  2. Including the std namespace so as to use its classes and functions without calling it.
  3. Calling the main() function inside which the logic of the program should be added. The { marks start of body of the main() function.
  4. Declaring an integer variable named choice.
  5. Printing some text on the console.
  6. Prompting the user to enter the value of choice.
  7. Using the switch statement and passing the argument choice to it. It means that we need to compare the value of variable choice to a set of other values.
  8. The { marks start of the switch body.
  9. Comparing the value of variable choice to a value of 1.
  10. Statement to be executed if the above case is true, that is, if choice is 10. The break prevents execution from continuing to the next case.
  11. Comparing the value of variable choice to a value of 2.
  12. Statement to be executed if the above case is true, that is, if choice is 2. The break prevents execution from continuing to the next case.
  13. Comparing the value of variable choice to a value of 3.
  14. Statement to be executed if above case is true, that is, if choice is 3. The break prevents execution from continuing to the next case.
  15. The default helps us state what to be done if the value of variable choice is not 1, 2, or 3.
  16. Statement to be executed if above cases are not true, that is, if choice is not 1, 2, or 3.
  17. End of the body of switch statement.
  18. End of the body of the main() function.

Summary

  • The switch statement helps us create a simple if…else…if ladder.
  • The switch statement has a clear and simple syntax than if…else…if ladder.
  • The switch statement should be used when you need to compare the value of a variable to a set of other values.
  • The values are added to case statements.
  • The break keywords stop the execution from continuing to the next case.
  • An optional default part is used to state action to be taken if no case is matched.