← Back to Core Java Course | Chapter 4: Control Flow | Lesson 3 of 9

Java की else-if Ladder

एक else-if ladder order में पूछे गए सवालों की एक series है जब तक एक true न हो, weather जांचने जैसा: sunny, cloudy, rainy? Java पहला match चलाता है और बाकी skip करता है।
Syntax
java
if (condition1) {
    // block 1
} else if (condition2) {
    // block 2
} else {
    // default
}

Basic else-if Ladder

एक else-if ladder top से bottom तक conditions की एक sequence test करता है, और जैसे ही एक true evaluate होता है, इसका block चलता है और इसके नीचे बची हर condition पूरी तरह skip हो जाती है।

उदाहरण: Basic else-if Ladder

java
public class Main {
	public static void main(String[] args) {
		int score = 85;
		if (score >= 90) {
			System.out.println("A");
		} else if (score >= 80) { // first true condition wins, rest skipped
			System.out.println("B");
		} else {
			System.out.println("C");
		}
	}
}

Multiple else-if Blocks

आप ज़रूरत के अनुसार कितने भी else-if clauses chain कर सकते हैं, जो इस pattern को एक value को कई ranges में से एक में categorize करने के लिए अच्छी तरह suited बनाता है, जैसे एक numeric score के आधार पर एक letter grade assign करना।

उदाहरण: Multiple else-if Blocks

java
public class Main {
	public static void main(String[] args) {
		int score = 72;
		if (score >= 90) {
			System.out.println("A");
		} else if (score >= 80) {
			System.out.println("B");
		} else if (score >= 70) {
			System.out.println("C"); // categorizes into a numeric range
		} else {
			System.out.println("F");
		}
	}
}

Importance of Order

चूंकि Java पहली true condition पर रुकता है, order मायने रखता है: सबसे specific या narrow conditions पहले रखें, क्योंकि बहुत जल्दी रखी एक broad condition इसके नीचे एक ज़्यादा specific branch के लिए meant cases catch कर लेगी।

उदाहरण: Importance of Order

java
public class Main {
	public static void main(String[] args) {
		int score = 95;
		if (score >= 90) { // most specific/narrow condition placed first
			System.out.println("A");
		} else if (score >= 0) { // broad condition placed after, or it would catch everything
			System.out.println("Other");
		}
	}
}

Exclusive Execution

पूरे ladder में बिल्कुल एक branch हर run में execute होता है, भले ही एक बाद की condition भी true evaluate होती। Ladder पहले match से आगे कुछ भी नहीं जांचता।

उदाहरण: Exclusive Execution

java
public class Main {
	public static void main(String[] args) {
		int score = 95;
		if (score >= 90) {
			System.out.println("A"); // only this branch runs, even though score >= 80 is also true
		} else if (score >= 80) {
			System.out.println("B");
		}
	}
}

Fallback Else Block

बिना किसी attached condition वाला एक trailing else हर उस value को catch करता है जो इसके ऊपर किसी explicit check से match न हुई हो, unexpected या unhandled input के लिए एक guaranteed fallback के रूप में काम करते हुए।

उदाहरण: Fallback Else Block

java
public class Main {
	public static void main(String[] args) {
		int score = -5;
		if (score >= 90) {
			System.out.println("A");
		} else if (score >= 80) {
			System.out.println("B");
		} else {
			System.out.println("Invalid score"); // catches anything unmatched above
		}
	}
}
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 को general से specific order में रखना, इसलिए score >= 50 उन values को catch कर लेता है जो score >= 90 को handle करनी चाहिए थीं।
  2. else if के बजाय अलग if statements उपयोग करना, इसलिए कई branches चल सकते हैं।
  3. आखिरी else छोड़ना, इसलिए unexpected values चुपचाप नज़रअंदाज़ हो जाती हैं।

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.