Java की else-if Ladder
In this page:
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
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Multiple else-if Blocks
आप ज़रूरत के अनुसार कितने भी else-if clauses chain कर सकते हैं, जो इस pattern को एक value को कई ranges में से एक में categorize करने के लिए अच्छी तरह suited बनाता है, जैसे एक numeric score के आधार पर एक letter grade assign करना।
उदाहरण: Multiple else-if Blocks
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Importance of Order
चूंकि Java पहली true condition पर रुकता है, order मायने रखता है: सबसे specific या narrow conditions पहले रखें, क्योंकि बहुत जल्दी रखी एक broad condition इसके नीचे एक ज़्यादा specific branch के लिए meant cases catch कर लेगी।
उदाहरण: Importance of Order
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Exclusive Execution
पूरे ladder में बिल्कुल एक branch हर run में execute होता है, भले ही एक बाद की condition भी true evaluate होती। Ladder पहले match से आगे कुछ भी नहीं जांचता।
उदाहरण: Exclusive Execution
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Fallback Else Block
बिना किसी attached condition वाला एक trailing else हर उस value को catch करता है जो इसके ऊपर किसी explicit check से match न हुई हो, unexpected या unhandled input के लिए एक guaranteed fallback के रूप में काम करते हुए।
उदाहरण: Fallback Else Block
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
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Conditions को general से specific order में रखना, इसलिए
score >= 50उन values को catch कर लेता है जोscore >= 90को handle करनी चाहिए थीं। else ifके बजाय अलगifstatements उपयोग करना, इसलिए कई branches चल सकते हैं।- आखिरी
elseछोड़ना, इसलिए unexpected values चुपचाप नज़रअंदाज़ हो जाती हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: