C++ का switch Statement
In this page:
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
Basic switch Statement
एक switch statement एक variable की value को कई possible constant cases से compare करता है और सीधे matching case पर jump करता है, जो एक single value को कई discrete options जैसे एक menu choice या day number से check करते समय एक लंबे else-if ladder से ज़्यादा cleanly पढ़ता है।
उदाहरण: Basic switch Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 3
int day = 3;
// Choose a branch based on the value of day
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
// Leave the switch/loop
break;
case 2:
std::cout << "Tuesday" << std::endl;
// Leave the switch/loop
break;
case 3:
std::cout << "Wednesday" << std::endl;
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
break Statement
break एक matching case का code चलने के बाद तुरंत switch block से exit हो जाता है; इसे छोड़ना execution को अगले case के code में भी 'fall through' कराता है, जो कभी-कभी intentional होता है लेकिन भूल जाने पर bugs का एक बहुत common source है।
जब तक fall-through आपके design का जानबूझकर हिस्सा न हो हमेशा break add करें।
उदाहरण: The break Statement
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 1
int day = 1;
// Choose a branch based on the value of day
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
case 2:
std::cout << "Falls through to Tuesday" << std::endl;
// Leave the switch/loop
break;
case 3:
std::cout << "Wednesday" << std::endl;
// Leave the switch/loop
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
default Case इस्तेमाल करना
default case किसी भी value को पकड़ता है जो explicit case labels में से किसी से match नहीं करती, एक if-else ladder में एक final else के switch statement equivalent की तरह काम करते हुए।
एक default case शामिल करना तब भी अच्छी practice है जब आप unmatched values की उम्मीद न करें, क्योंकि यह unexpected input को चुपचाप ignore होने की बजाय visible बनाता है।
उदाहरण: Using the default Case
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 9
int day = 9;
// Choose a branch based on the value of day
switch (day) {
case 1:
std::cout << "Monday" << std::endl;
// Leave the switch/loop
break;
// default runs when no case matches
default:
std::cout << "Invalid day" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
switch Cases Group करना
एक shared block के ठीक ऊपर कई case labels stack करना, जैसे case 1: case 2: case 3: के बाद code का एक block, कई distinct values को हर एक के लिए code duplicate किए बिना identical behavior trigger करने देता है।
यह same fall-through behavior पर depend करता है जिसे break normally रोकता है, यहाँ जानबूझकर इस्तेमाल किया गया।
उदाहरण: Grouping switch Cases
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare day and set it to 6
int day = 6;
// Choose a branch based on the value of day
switch (day) {
case 1:
case 2:
case 3:
case 4:
case 5:
std::cout << "Weekday" << std::endl;
// Leave the switch/loop
break;
case 6:
case 7:
std::cout << "Weekend" << std::endl;
// Leave the switch/loop
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Nested switch Statements
एक switch statement को दूसरे switch के case के अंदर रखना layered categorical decisions handle करने के लिए valid है, जैसे पहले एक product category पर और फिर इसके अंदर एक specific product पर branch करना।
Nested switches जल्दी follow करना मुश्किल हो सकते हैं, इसलिए यह consider करना worth है कि अलग functions logic को ज़्यादा clear रखेंगे या नहीं।
उदाहरण: Nested switch Statements
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare category and set it to 1
int category = 1;
// Declare size and set it to 2
int size = 2;
// Choose a branch based on the value of category
switch (category) {
case 1:
switch (size) {
case 1:
std::cout << "Small shirt" << std::endl;
// Leave the switch/loop
break;
case 2:
std::cout << "Medium shirt" << std::endl;
// Leave the switch/loop
break;
}
break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
breakभूल जाना, ताकि execution अगले case में fall through हो जाए।- एक
caseमें एक non-constant या string इस्तेमाल करना, जब case labels को integral constants होना चाहिए। - braces के बिना एक case के अंदर एक variable declare करना, जो एक 'jump to case label' error cause कर सकता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: