🟡 Control Flow  ·  Lesson 16

switch-case Statement

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.

switch क्या है?

जब आपको एक variable की कई निश्चित values से तुलना करनी हो, लंबी if-else श्रृंखला पढ़ना कठिन हो जाता है। switch statement एक साफ़ तरीका है: यह सीधे मेल खाने वाले case पर कूद जाता है।

बुनियादी syntax

C++
switch (value) {
    case 1:
        // value == 1 hone par chalta hai
        break;
    case 2:
        // value == 2 hone par chalta hai
        break;
    default:
        // kuchh match na hone par chalta hai
}

एक चलता उदाहरण

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

break क्यों मायने रखता है

हर case आमतौर पर break से खत्म होना चाहिए। अगर आप इसे भूलें, C++ अगले cases में चलता रहता है — एक bug जिसे "fall-through" कहते हैं। बिना breaks के देखें क्या होता है:

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

चूँकि कोई breaks नहीं हैं, मेल खाने वाला case 1 उसके बाद का सब कुछ भी चला देता है।

जानबूझकर fall-through

कभी fall-through उपयोगी होता है — कई cases को एक 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 बनाम if-else

switch तब जबif-else तब जब
एक variable बनाम कई निश्चित valuesConditions में ranges हों
Values ints, chars या enums होंआप strings या doubles की तुलना करें
Menu choices, day numbersकई variables या logic

आम गलतियाँ

  • break भूलना, आकस्मिक fall-through पैदा करना।
  • string या double पर switch करने की कोशिश (अनुमति नहीं)।
  • Variable को case label के रूप में इस्तेमाल करना — labels constants होने चाहिए।
  • default छोड़ देना, तो अप्रत्याशित values चुपचाप अनदेखी हो जाती हैं।
🏋️ अभ्यास

एक सरल calculator लिखें: दो numbers और एक operator character (+ - * /) पढ़ें, फिर operator पर switch से परिणाम print करें। अमान्य operator के लिए एक default शामिल करें।

सारांश

  • switch value को case labels से मिलाकर block चुनता है।
  • break case समाप्त करता है; इसके बिना execution fall through होता है।
  • default उन values को संभालता है जो किसी case से मेल न खाएँ।
  • switch ints, chars और enums के साथ काम करता है — strings या doubles नहीं।
  • कई निश्चित values के लिए switch पसंद करें; ranges और जटिल logic के लिए if-else।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C++ में switch statement क्या है?
switch statement किसी variable की value के आधार पर चलाने को एक code block चुनता है। यह value की कई case labels से तुलना करता है और मेल खाने वाला चलाता है, एक variable को निश्चित values से जाँचते समय लंबी if-else श्रृंखला का साफ़ विकल्प देते हुए।
switch statement में break क्यों इस्तेमाल होता है?
मेल खाने वाला case चलने के बाद break switch समाप्त करता है। इसके बिना execution "fall through" होकर नीचे के cases भी चलाता रहता है। तो आप आमतौर पर हर case के अंत में break रखते हैं जब तक जानबूझकर fall-through न चाहें।
switch में default case क्या है?
default case तब चलता है जब कोई भी दूसरी case label value से मेल न खाए। यह switch के else जैसा काम करता है, आपको अप्रत्याशित values संभालने देते हुए। यह वैकल्पिक है पर आमतौर पर शामिल करना अच्छी आदत है।
क्या C++ में switch strings के साथ इस्तेमाल हो सकता है?
नहीं। C++ switch केवल integer types और characters (char भीतर से integer है) तथा enum values के साथ काम करता है। यह std::string या floating-point numbers पर switch नहीं कर सकता — उनके लिए आप if-else श्रृंखला इस्तेमाल करते हैं।
C++ में if-else के बजाय switch कब इस्तेमाल करूँ?
switch तब इस्तेमाल करें जब आप एक variable की कई निश्चित, constant values से तुलना कर रहे हों, जैसे menu choice या day number — यह साफ़ और अक्सर तेज़ है। if-else तब इस्तेमाल करें जब conditions में ranges, कई variables या जटिल expressions हों।
← Back to C++ Tutorial
🔗

Share this topic with a friend

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

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

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

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.