C++ Switch Case Statement with Program EXAMPLES

โšก Smart Summary

A switch statement in C++ selects one block of code to run from many options based on the value of a variable. Here you will learn how a switch works, its syntax, the break keyword, and examples.

  • ๐Ÿ”€ What it is: A switch picks one case to run based on a variable’s value.
  • ๐Ÿงพ Syntax: switch(expr) with case labels, break, and an optional default.
  • โน๏ธ break: break stops execution from falling through to the next case.
  • ๐ŸŽฏ When to use: Use it to replace long if-else chains on one variable.
  • ๐Ÿค– AI help: AI tools like GitHub Copilot write and check switch statements.

C++ Switch Statement

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.

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.

Switch Case Program 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:

Switch Case Program

Here is a screenshot of the code:

Switch Case Program

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.

Also Check our article on Difference Between C and C++:- Click Here

Switch Case Program 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:

Switch Case Program

Here is a screenshot of the code:

Switch Case Program

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.

FAQs

A switch statement selects one block of code to run from several cases, based on the value of a single variable or expression.

The syntax uses switch(expression) with one or more case labels, a break after each case, and an optional default case for unmatched values.

break ends the switch and stops execution from falling through into the next case. Without it, the cases that follow also run.

Use a switch when you compare one variable against many fixed values, as it is clearer than a long chain of if-else statements.

A switch tests one variable against constant values, while if-else can test ranges and complex conditions. Switch is often cleaner for many fixed choices.

The default case runs when no case matches the expression. It is optional and usually placed last to handle unexpected values.

AI assistants like GitHub Copilot generate switch statements from a comment, add missing break statements, and convert long if-else chains into switches.

Yes. AI-powered tools flag missing break statements, unreachable cases, and absent default cases, helping your C++ switch logic run correctly.

Summarize this post with: