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
switch (value) {
case 1:
// runs if value == 1
break;
case 2:
// runs if value == 2
break;
default:
// runs if nothing matched
}A working example
#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;
}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:
int x = 1;
switch (x) {
case 1: cout << "one ";
case 2: cout << "two ";
case 3: cout << "three ";
}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:
char c = 'e';
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "Vowel\n";
break;
default:
cout << "Consonant\n";
}Vowel
switch vs if-else
| Use switch when | Use if-else when |
|---|---|
| One variable vs many fixed values | Conditions involve ranges |
| Values are ints, chars or enums | You compare strings or doubles |
| Menu choices, day numbers | Multiple variables or logic |
Common mistakes
- Forgetting
break, causing accidental fall-through. - Trying to
switchon astringordouble(not allowed). - Using a variable as a
caselabel — labels must be constants. - Leaving out
default, so unexpected values are silently ignored.
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
switchpicks a block by matching a value tocaselabels.breakends a case; without it, execution falls through.defaulthandles 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++?
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?
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++?
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++?
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.switch क्या है?
जब आपको एक variable की कई निश्चित values से तुलना करनी हो, लंबी if-else श्रृंखला पढ़ना कठिन हो जाता है। switch statement एक साफ़ तरीका है: यह सीधे मेल खाने वाले case पर कूद जाता है।
बुनियादी syntax
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
}एक चलता उदाहरण
#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;
}Wednesday
break क्यों मायने रखता है
हर case आमतौर पर break से खत्म होना चाहिए। अगर आप इसे भूलें, C++ अगले cases में चलता रहता है — एक bug जिसे "fall-through" कहते हैं। बिना breaks के देखें क्या होता है:
int x = 1;
switch (x) {
case 1: cout << "one ";
case 2: cout << "two ";
case 3: cout << "three ";
}one two three
चूँकि कोई breaks नहीं हैं, मेल खाने वाला case 1 उसके बाद का सब कुछ भी चला देता है।
जानबूझकर fall-through
कभी fall-through उपयोगी होता है — कई cases को एक block साझा करने देना:
char c = 'e';
switch (c) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
cout << "Vowel\n";
break;
default:
cout << "Consonant\n";
}Vowel
switch बनाम if-else
| switch तब जब | if-else तब जब |
|---|---|
| एक variable बनाम कई निश्चित values | Conditions में ranges हों |
| Values ints, chars या enums हों | आप strings या doubles की तुलना करें |
| Menu choices, day numbers | कई variables या logic |
आम गलतियाँ
breakभूलना, आकस्मिक fall-through पैदा करना।stringयाdoubleपरswitchकरने की कोशिश (अनुमति नहीं)।- Variable को
caselabel के रूप में इस्तेमाल करना — labels constants होने चाहिए। defaultछोड़ देना, तो अप्रत्याशित values चुपचाप अनदेखी हो जाती हैं।
एक सरल calculator लिखें: दो numbers और एक operator character (+ - * /) पढ़ें, फिर operator पर switch से परिणाम print करें। अमान्य operator के लिए एक default शामिल करें।
सारांश
switchvalue कोcaselabels से मिलाकर block चुनता है।breakcase समाप्त करता है; इसके बिना execution fall through होता है।defaultउन values को संभालता है जो किसी case से मेल न खाएँ।- switch ints, chars और enums के साथ काम करता है — strings या doubles नहीं।
- कई निश्चित values के लिए switch पसंद करें; ranges और जटिल logic के लिए if-else।