Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🟡 Control Flow  ·  Lesson 16

switch Statement in C++

In this lesson: Learn switch-case in C++ with break, default and a menu calculator program — with output. In Hindi & English.

What is switch?

When you need to compare one variable against many fixed values, a long if-else chain gets hard to read. The switch statement is a cleaner way: it jumps straight to the matching case.

Basic syntax

C++
switch (value) {
    case 1:
        // runs if value == 1
        break;
    case 2:
        // runs if value == 2
        break;
    default:
        // runs if nothing matched
}

A working example

C++
#include <iostream>
using namespace std;
int main() {
    int day = 3;
    switch (day) {
        case 1: cout << "Monday\n";    break;
        case 2: cout << "Tuesday\n";   break;
        case 3: cout << "Wednesday\n"; break;
        default: cout << "Other day\n";
    }
    return 0;
}
Output:
Wednesday

Why break matters

Each case should usually end with break. If you forget it, C++ keeps running into the next cases — a bug called "fall-through." Watch what happens without breaks:

C++
int x = 1;
switch (x) {
    case 1: cout << "one ";
    case 2: cout << "two ";
    case 3: cout << "three ";
}
Output:
one two three

Because there are no breaks, matching case 1 runs everything after it too.

Deliberate fall-through

Sometimes fall-through is useful — letting several cases share one block:

C++
char c = 'e';
switch (c) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
        cout << "Vowel\n";
        break;
    default:
        cout << "Consonant\n";
}
Output:
Vowel

switch vs if-else

Use switch whenUse if-else when
One variable vs many fixed valuesConditions involve ranges
Values are ints, chars or enumsYou compare strings or doubles
Menu choices, day numbersMultiple variables or logic

Common mistakes

  • Forgetting break, causing accidental fall-through.
  • Trying to switch on a string or double (not allowed).
  • Using a variable as a case label — labels must be constants.
  • Leaving out default, so unexpected values are silently ignored.
🏋️ Practice

Write a simple calculator: read two numbers and an operator character (+ - * /), then use a switch on the operator to print the result. Include a default for an invalid operator.

Summary

  • switch picks a block by matching a value to case labels.
  • break ends a case; without it, execution falls through.
  • default handles values that match no case.
  • switch works with ints, chars and enums — not strings or doubles.
  • Prefer switch for many fixed values; if-else for ranges and complex logic.

Frequently Asked Questions

What is a switch statement in C++?
A switch statement chooses one block of code to run based on the value of a variable. It compares the value against several case labels and runs the matching one, offering a cleaner alternative to a long chain of if-else statements when checking one variable against fixed values.
Why is break used in a switch statement?
break ends the switch once a matching case has run. Without it, execution "falls through" and keeps running the cases below too. So you normally put a break at the end of each case unless you deliberately want fall-through.
What is the default case in a switch?
The default case runs when none of the other case labels match the value. It acts like the else of a switch, letting you handle unexpected values. It is optional but usually good practice to include.
Can switch be used with strings in C++?
No. A C++ switch works only with integer types and characters (a char is an integer underneath) and enum values. It cannot switch on std::string or floating-point numbers — for those you use an if-else chain instead.
When should I use switch instead of if-else in C++?
Use switch when you are comparing one variable against many fixed, constant values, such as a menu choice or a day number — it is clearer and often faster. Use if-else when conditions involve ranges, multiple variables or complex expressions.
← Back to C++ Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicfor Loop in C++ Your progress: 16 / 81 (20%) · Lesson 17 / 81 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free