C++ की else-if Ladder
In this page:
if (condition1) {
// block 1
} else if (condition2) {
// block 2
} else {
// default block
}
Basic else-if Ladder
एक else-if ladder top से bottom तक conditions की एक sequence check करता है और सिर्फ पहले वाले का block चलाता है जो true evaluate हो, इसके बाद की हर condition skip करते हुए।
यह इसे कई mutually exclusive possibilities को order में test करने के लिए natural choice बनाता है, जैसे एक numeric score के आधार पर एक letter grade assign करना।
उदाहरण: Basic else-if Ladder
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 75
int score = 75;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 70) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else {
std::cout << "C" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
कई else-if Blocks
आप initial if के बाद ज़रूरत के हिसाब से कितने भी else if blocks chain कर सकते हैं, जो deeply nested if statements के बिना कई distinct categories या numeric ranges check करने के लिए अच्छे से scale करता है।
हर additional else if सिर्फ तभी evaluate होता है जब इसके ऊपर की हर condition false रही हो।
उदाहरण: Multiple else-if Blocks
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 65
int score = 65;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 80) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else if (score >= 70) {
std::cout << "C" << std::endl;
} else if (score >= 60) {
std::cout << "D" << std::endl;
} else {
std::cout << "F" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Conditions का Order मायने रखता है
क्योंकि ladder पहली true condition पर रुक जाता है, order बहुत मायने रखता है — score >= 90 जैसी narrower condition से पहले score >= 60 जैसी broad condition रखना galat branch में high scores पकड़ लेगा।
जब ranges overlap करें हमेशा conditions को सबसे specific से सबसे general तक order करें।
उदाहरण: Order of Conditions matters
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
// Check whether score >= 60
if (score >= 60) {
// Print to the console with cout
std::cout << "Pass (matches too early)" << std::endl;
} else if (score >= 90) {
// Print to the console with cout
std::cout << "A (never reached)" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Ladder बनाम Single Ifs
एक single else-if ladder guarantee करता है कि ज़्यादा से ज़्यादा एक block चले, क्योंकि evaluation एक true condition मिलते ही रुक जाता है, जबकि कई अलग, independent if statements लिखना हर condition test करेगा चाहे कुछ भी हो और एक से ज़्यादा block execute कर सकता है।
एक ladder specifically तब चुनें जब branches mutually exclusive outcomes represent करें।
उदाहरण: Ladder vs Single Ifs
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 95
int score = 95;
if (score >= 90) std::cout << "A" << std::endl;
else if (score >= 70) std::cout << "B" << std::endl;
if (score >= 90) std::cout << "Also true (separate check)" << std::endl;
if (score >= 70) std::cout << "Also true (separate check)" << std::endl;
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Else से Default Fallback
बिना किसी condition वाला एक trailing else ladder के catch-all की तरह काम करता है, सिर्फ तभी चलता है जब इसके ऊपर की हर single condition false निकली हो — यह unexpected या invalid input handle करने की natural जगह है।
इसे छोड़ने का मतलब है अगर कोई condition match न करे, program बस कुछ भी नहीं करता, जो चुपचाप bugs छिपा सकता है।
उदाहरण: Default Fallback with Else
// Include std::cout and std::cin
#include <iostream>
// Program execution starts in main()
int main() {
// Declare score and set it to 40
int score = 40;
// Check whether score >= 90
if (score >= 90) {
// Print to the console with cout
std::cout << "A" << std::endl;
} else if (score >= 70) {
// Print to the console with cout
std::cout << "B" << std::endl;
} else {
std::cout << "Fail" << std::endl;
}
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- conditions को broad से specific order में रखना, जैसे
score >= 90से पहलेscore >= 50, ताकि पहला test हमेशा जीते। else ifकोelseifकी तरह लिखना, जो valid C++ नहीं है।- एक ladder की बजाय अलग
ifstatements इस्तेमाल करना, ताकि एक से ज़्यादा branch चल सके।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: