C का switch Statement
switch (variable) {
case value1:
// code
break;
case value2:
// code
break;
default:
// code if no case matches
}
switch Statement क्या है?
एक switch statement एक expression को एक बार evaluate करता है और इसे case labels की एक list से compare करता है, सीधे matching case पर jump करते हुए -- जब आप एक variable को कई fixed values से compare कर रहे हों तो एक लंबे else-if ladder का एक cleaner alternative।
उदाहरण: What is a switch Statement?
// Include standard input/output (printf, scanf)
#include <stdio.h>
// 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 3:
printf("Wednesday");
// 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
Day of the Week
1 से 7 तक integers को day names पर map करना एक classic switch use case है: हर case label एक specific number handle करता है, mapping को equivalent if-else comparisons की chain से कहीं ज़्यादा readable बनाते हुए।
उदाहरण: Day of the Week
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare day and set it to 2
int day = 2;
// Choose a branch based on the value of day
switch (day) {
case 1: printf("Monday"); break;
case 2: printf("Tuesday"); break;
case 3: printf("Wednesday"); break;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Grade Switch
switch character constants भी match कर सकता है, इसलिए एक grade switch case A को "Excellent" print करने के लिए map कर सकता है, आपको integers जितनी ही आसानी से single-character input पर branch करने देते हुए।
उदाहरण: Grade Switch
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare grade and set it to 'A'
char grade = 'A';
// Choose a branch based on the value of grade
switch (grade) {
case 'A':
printf("Excellent");
// 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
break Keyword
एक case block के अंत में explicit break के बिना, execution "fall through" होता है और अगले case में भी code चलाना जारी रखता है -- ऐसी language से आने वाले किसी के लिए एक frequent source of bugs जहाँ fall-through default नहीं है।
उदाहरण: The break Keyword
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 1
int x = 1;
// Choose a branch based on the value of x
switch (x) {
case 1:
printf("One ");
case 2:
printf("Falls through to Two");
// 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 तब चलता है जब switch expression listed case values में से किसी से match नहीं करता, एक catch-all की तरह काम करते हुए -- unexpected input handle करने या invalid values explicitly flag करने के लिए useful।
उदाहरण: The default Case
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Program execution starts in main()
int main() {
// Declare x and set it to 9
int x = 9;
// Choose a branch based on the value of x
switch (x) {
case 1:
printf("One");
// Leave the switch/loop
break;
// default runs when no case matches
default:
printf("Unknown value");
}
// 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 हो जाए।- एक
caselabel की तरह एक string या variable इस्तेमाल करना, जब labels को integer constants होना चाहिए। - unexpected values के लिए
defaultcase भूल जाना।
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: