← Back to C++ Course | Chapter 4: Control Flow | Lesson 5 of 13

C++ की else-if Ladder

एक else-if ladder questions को order में तब तक पूछता है जब तक एक true न हो, weather check करने जैसा: sunny, cloudy, rainy? C++ पहला match चलाता है और बाकी skip कर देता है।
Syntax
cpp
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

cpp
// 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;
}

कई 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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}

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

cpp
// 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;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. conditions को broad से specific order में रखना, जैसे score >= 90 से पहले score >= 50, ताकि पहला test हमेशा जीते।
  2. else if को elseif की तरह लिखना, जो valid C++ नहीं है।
  3. एक ladder की बजाय अलग if statements इस्तेमाल करना, ताकि एक से ज़्यादा branch चल सके।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.