codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
⚡

C++ Programming

15 / 87 topics
14if, if...else, and Nested if...else15switch..case Statement16Loops17Jump Statements: break, continue, goto
Tutorials/C++ Programming/switch..case Statement
⚡C++ Programming

switch..case Statement

Updated 2026-05-12
15 min read

switch..case Statement

In this tutorial, we will explore the switch statement in C++, a powerful tool for handling multiple conditions based on a single variable. The switch statement is particularly useful when you have a set of discrete values and want to execute different blocks of code depending on which value matches.

Introduction

The switch statement provides an alternative to using multiple if-else statements, especially when dealing with several distinct cases. It makes the code more readable and easier to maintain by grouping related conditions together.

Core Content

Syntax Overview

The basic syntax of a switch statement is as follows:

C++
1switch (expression) {
2 case constant1:
3 // Code block executed if expression == constant1
4 break;
5 case constant2:
6 // Code block executed if expression == constant2
7 break;
8 default:
9 // Code block executed if none of the above cases match
10}
  • expression: An integer, character, or enumerated type.
  • case: Each case label is followed by a constant value and a colon (:).
  • break: The break statement terminates the switch statement and exits the block. If omitted, control will "fall through" to the next case.
  • default: The default case is optional and acts as an "else" clause for all other cases.

Example 1: Basic Usage

Let's start with a simple example where we use a switch statement to determine the day of the week based on an integer input.

day.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int day = 3;
6
7 switch (day) {
8 case 1:
9 cout << "Monday";
10 break;
11 case 2:
12 cout << "Tuesday";
13 break;
14 case 3:
15 cout << "Wednesday";
16 break;
17 case 4:
18 cout << "Thursday";
19 break;
20 case 5:
21 cout << "Friday";
22 break;
23 default:
24 cout << "Weekend";
25 }
26
27 return 0;
28}
Output
Wednesday

Example 2: Fall-through

In the absence of a break statement, control will fall through to the next case. This can be useful if multiple cases share the same code block.

fallthrough.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int grade = 85;
6
7 switch (grade / 10) {
8 case 9:
9 cout << "Grade: A";
10 break;
11 case 8:
12 cout << "Grade: B";
13 break;
14 case 7:
15 cout << "Grade: C";
16 break;
17 default:
18 cout << "Grade: F";
19 }
20
21 return 0;
22}
Output
Grade: B

Example 3: Multiple Cases

You can group multiple cases together if they share the same code block.

multiple_cases.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int month = 12;
6
7 switch (month) {
8 case 1:
9 case 2:
10 case 3:
11 cout << "Winter";
12 break;
13 case 4:
14 case 5:
15 case 6:
16 cout << "Spring";
17 break;
18 case 7:
19 case 8:
20 case 9:
21 cout << "Summer";
22 break;
23 case 10:
24 case 11:
25 case 12:
26 cout << "Autumn";
27 break;
28 }
29
30 return 0;
31}
Output
Autumn

Example 4: Using switch with Characters

The switch statement can also be used with characters.

char_switch.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 char grade = 'B';
6
7 switch (grade) {
8 case 'A':
9 cout << "Excellent!";
10 break;
11 case 'B':
12 cout << "Well done";
13 break;
14 case 'C':
15 cout << "You passed";
16 break;
17 default:
18 cout << "Better try again";
19 }
20
21 return 0;
22}
Output
Well done

Best Practices

  • Use break statements: Always include a break statement at the end of each case to prevent fall-through.
  • Avoid fall-through intentionally: If you do use fall-through, make sure it's intentional and well-documented.
  • Order cases logically: Arrange cases in a logical order for better readability.

Common Mistakes

  • Missing break statements: Forgetting to include break can lead to unexpected behavior (fall-through).
  • Using non-integer types: The expression must be an integer, character, or enumerated type. Avoid using floating-point numbers.
  • Incorrect case values: Ensure that the case values are constants and match the data type of the expression.

Comparison with if-else

The switch statement is often used as a more readable alternative to nested if-else statements when dealing with multiple discrete conditions. Here's a comparison:

Featureswitch Statementif-else Statements
SyntaxMore concise for multiple casesLess concise for multiple conditions
ReadabilityEasier to read with many discrete casesCan become cumbersome with many cases
PerformanceGenerally faster due to jump tableMay be slower due to sequential checks

Practical Example

Let's create a simple program that determines the type of fruit based on user input.

fruit.cpp
1#include <iostream>
2using namespace std;
3
4int main() {
5 int choice;
6
7 cout << "Enter the number corresponding to your favorite fruit:" << endl;
8 cout << "1. Apple" << endl;
9 cout << "2. Banana" << endl;
10 cout << "3. Orange" << endl;
11 cout << "4. Other" << endl;
12
13 cin >> choice;
14
15 switch (choice) {
16 case 1:
17 cout << "You selected Apple!";
18 break;
19 case 2:
20 cout << "You selected Banana!";
21 break;
22 case 3:
23 cout << "You selected Orange!";
24 break;
25 default:
26 cout << "You selected Other fruit!";
27 }
28
29 return 0;
30}
Output
Enter the number corresponding to your favorite fruit:
1. Apple
2. Banana
3. Orange
4. Other
2
You selected Banana!

Summary

Key PointsDescription
Syntaxswitch (expression) { case constant: code; break; default: code; }
BenefitsCleaner and more efficient for multiple discrete conditions
Best PracticesUse break statements, avoid fall-through unless intentional
Common MistakesMissing break, using non-integer types, incorrect case values
ComparisonMore concise than nested if-else for many cases

What's Next?

Now that you've learned about the switch statement, it's time to explore loops in C++. Loops allow you to execute a block of code repeatedly under certain conditions. In the next tutorial, we will cover different types of loops such as for, while, and do-while.

Stay tuned!


Previousif, if...else, and Nested if...elseNext Loops

Recommended Gear

if, if...else, and Nested if...elseLoops