---
description: This C++ Switch Case tutorial will teach all the basic to advanced concepts. Learn What is Switch, When to use, Break Keyword with syntax and examples.
title: C++ Switch Case Statement with Program EXAMPLES
image: https://www.guru99.com/images/cpp-switch-case-statement.png
---

[Skip to content](#main)

**⚡ 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.

Read More

![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]()](https://www.guru99.com/images/2/030720_0456_CSwitchCase1.png)

Here is a screenshot of the code:

[![Switch Case Program]()](https://www.guru99.com/images/2/030720_0456_CSwitchCase2.png)

**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](https://www.guru99.com/cpp-functions.html) 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](https://www.guru99.com/c-vs-cpp.html)

**Don't Miss:**

- [What is C++? Basic Concepts of C++ Programming Language](https://www.guru99.com/cpp-tutorial.html)
- [Arrays in C++: Declare, Initialize & Pointers (Examples)](https://www.guru99.com/arrays-in-cpp-functions.html)
- [C++ Exception Handling: Try, Catch, throw Example](https://www.guru99.com/cpp-exceptions-handling.html)
- [C++ Functions with Program Examples](https://www.guru99.com/cpp-functions.html)

## 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]()](https://www.guru99.com/images/2/030720_0456_CSwitchCase3.png)

Here is a screenshot of the code:

[![Switch Case Program]()](https://www.guru99.com/images/2/030720_0456_CSwitchCase4.png)

**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

🔀 What is a switch statement in C++?

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

🧾 What is the syntax of a switch statement?

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

⏹️ What does the break keyword do in a switch?

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

🎯 When should you use a switch statement?

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

🆚 What is the difference between switch and if-else?

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.

❓ What is the default case in a switch?

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

🤖 How can AI tools like GitHub Copilot help with switch statements?

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

🧠 Can AI catch switch statement bugs?

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:

ChatGPTPerplexityGrokGoogle AI

**Stay Updated on AI** **Get Weekly AI Skills, Trends, Actionable Advice.**

##### Sign up for the newsletter

Subscribe for Free

You have successfully subscribed.  
Please check your inbox.

![AI-Newsletter]() Chosen by over **350,000+** professionals

[Scroll to top](#wrapper)

×

Search for:

```json
{"@context":"https://schema.org","@graph":[{"@type":"Organization","@id":"https://www.guru99.com/#organization","name":"Guru99","sameAs":["https://www.facebook.com/Guru99Official","https://twitter.com/guru99com"],"logo":{"@type":"ImageObject","@id":"https://www.guru99.com/#logo","url":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","contentUrl":"https://www.guru99.com/images/guru99-logo-v1-150x59.png","caption":"Guru99","inLanguage":"en-US"}},{"@type":"WebSite","@id":"https://www.guru99.com/#website","url":"https://www.guru99.com","name":"Guru99","publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US"},{"@type":"ImageObject","@id":"https://www.guru99.com/images/cpp-switch-case-statement.png","url":"https://www.guru99.com/images/cpp-switch-case-statement.png","width":"700","height":"250","caption":"C++ Switch Case Statement","inLanguage":"en-US"},{"@type":"BreadcrumbList","@id":"https://www.guru99.com/cpp-switch-example.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":"1","item":{"@id":"https://www.guru99.com","name":"Home"}},{"@type":"ListItem","position":"2","item":{"@id":"https://www.guru99.com/cpp","name":"CPP"}},{"@type":"ListItem","position":"3","item":{"@id":"https://www.guru99.com/cpp-switch-example.html","name":"C++ Switch Case Statement with Program EXAMPLES"}}]},{"@type":"WebPage","@id":"https://www.guru99.com/cpp-switch-example.html#webpage","url":"https://www.guru99.com/cpp-switch-example.html","name":"C++ Switch Case Statement with Program EXAMPLES","dateModified":"2026-08-06T13:17:36+05:30","isPartOf":{"@id":"https://www.guru99.com/#website"},"primaryImageOfPage":{"@id":"https://www.guru99.com/images/cpp-switch-case-statement.png"},"inLanguage":"en-US","breadcrumb":{"@id":"https://www.guru99.com/cpp-switch-example.html#breadcrumb"}},{"@type":"Person","@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker","description":"I'm Benjamin Walker, an expert in C, C++, and C# programming, providing resources to enhance your coding proficiency and project outcomes.","url":"https://www.guru99.com/author/benjamin","image":{"@type":"ImageObject","@id":"https://www.guru99.com/images/benjamin-walker-author.png","url":"https://www.guru99.com/images/benjamin-walker-author.png","caption":"Benjamin Walker","inLanguage":"en-US"},"worksFor":{"@id":"https://www.guru99.com/#organization"}},{"articleSection":"CPP","headline":"C++ Switch Case Statement with Program EXAMPLES","description":"This C++ Switch Case tutorial will teach all the basic to advanced concepts. Learn What is Switch, When to use, Break Keyword with syntax and examples.","keywords":"bigdata, programming, database, server","speakable":{"@type":"SpeakableSpecification","cssSelector":[".entry-title",".summary"]},"@type":"Article","author":{"@id":"https://www.guru99.com/author/benjamin","name":"Benjamin Walker"},"dateModified":"2026-08-06T13:17:36+05:30","image":{"@id":"https://www.guru99.com/images/cpp-switch-case-statement.png"},"copyrightYear":"2026","name":"C++ Switch Case Statement with Program EXAMPLES","subjectOf":[{"@type":"FAQPage","mainEntity":[{"@type":"Question","name":"What is a switch statement in C++?","acceptedAnswer":{"@type":"Answer","text":"A switch statement selects one block of code to run from several cases, based on the value of a single variable or expression."}},{"@type":"Question","name":"What is the syntax of a switch statement?","acceptedAnswer":{"@type":"Answer","text":"The syntax uses switch(expression) with one or more case labels, a break after each case, and an optional default case for unmatched values."}},{"@type":"Question","name":"What does the break keyword do in a switch?","acceptedAnswer":{"@type":"Answer","text":"break ends the switch and stops execution from falling through into the next case. Without it, the cases that follow also run."}},{"@type":"Question","name":"When should you use a switch statement?","acceptedAnswer":{"@type":"Answer","text":"Use a switch when you compare one variable against many fixed values, as it is clearer than a long chain of if-else statements."}},{"@type":"Question","name":"What is the difference between switch and if-else?","acceptedAnswer":{"@type":"Answer","text":"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."}},{"@type":"Question","name":"What is the default case in a switch?","acceptedAnswer":{"@type":"Answer","text":"The default case runs when no case matches the expression. It is optional and usually placed last to handle unexpected values."}},{"@type":"Question","name":"How can AI tools like GitHub Copilot help with switch statements?","acceptedAnswer":{"@type":"Answer","text":"AI assistants like GitHub Copilot generate switch statements from a comment, add missing break statements, and convert long if-else chains into switches."}},{"@type":"Question","name":"Can AI catch switch statement bugs?","acceptedAnswer":{"@type":"Answer","text":"Yes. AI-powered tools flag missing break statements, unreachable cases, and absent default cases, helping your C++ switch logic run correctly."}}]}],"@id":"https://www.guru99.com/cpp-switch-example.html#schema-1142551","isPartOf":{"@id":"https://www.guru99.com/cpp-switch-example.html#webpage"},"publisher":{"@id":"https://www.guru99.com/#organization"},"inLanguage":"en-US","mainEntityOfPage":{"@id":"https://www.guru99.com/cpp-switch-example.html#webpage"}}]}
```
