Java का if-else Statement
In this page:
if (condition) {
// if true
} else {
// if false
}
Standard If-Else
if-else गारंटी देता है कि इसके दो blocks में से बिल्कुल एक चलता है: if-block जब condition true हो, या else-block एक fallback के रूप में जब भी यह false हो -- कोई case नहीं जहां दोनों में से कोई न चले या दोनों चलें।
उदाहरण: Standard If-Else
public class Main {
public static void main(String[] args) {
int age = 15;
if (age >= 18) {
System.out.println("Adult");
} else {
System.out.println("Minor"); // exactly one of these always runs
}
}
}
Login to try C/C++/Java/PHP code in the editor
Conditional Assignments
इसे भरने के लिए एक empty variable के बाद एक अलग if-else block के बजाय, आप हर branch के अंदर सीधे एक value compute और assign कर सकते हैं, decision और assignment को visually साथ रखते हुए।
उदाहरण: Conditional Assignments
public class Main {
public static void main(String[] args) {
int score = 55;
String result;
if (score >= 60) {
result = "Pass"; // computed and assigned inside each branch
} else {
result = "Fail";
}
System.out.println(result);
}
}
Login to try C/C++/Java/PHP code in the editor
Checking Boolean Toggles
Condition के रूप में एक single boolean variable, जैसे if (isPremium) {...} else {...}, लगभग plain English जैसा पढ़ता है और एक feature flag या account status के आधार पर behavior branch करने का सबसे clean तरीका है।
उदाहरण: Checking Boolean Toggles
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare isPremium and set it to true
boolean isPremium = true;
// Check whether isPremium
if (isPremium) {
// Print a line to the console
System.out.println("Premium features unlocked");
} else {
// Print a line to the console
System.out.println("Standard features only");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Complex Conditions in If-Else
Condition के अंदर && और || combine करना एक if-else को compound business rules पर react करने देता है, जैसे किसी action की allow करने से पहले एक minimum age AND parental consent दोनों मांगना।
उदाहरण: Complex Conditions in If-Else
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Declare age and set it to 16
int age = 16;
// Declare hasParentalConsent and set it to true
boolean hasParentalConsent = true;
// Run this block only if the condition is true
if (age >= 18 || (age >= 13 && hasParentalConsent)) {
// Print a line to the console
System.out.println("Allowed");
} else {
// Print a line to the console
System.out.println("Not allowed");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested If-Else Blocks
if-else blocks को एक-दूसरे के अंदर nest करना एक असली decision tree बनाता है, जहां हर level possibilities को आगे narrow करता है -- उपयोगी, हालांकि 2-3 levels गहराई से आगे इसके बजाय इसे एक else-if ladder या switch में refactor करना अक्सर clearer है।
उदाहरण: Nested If-Else Blocks
public class Main {
public static void main(String[] args) {
int score = 72;
if (score >= 90) {
System.out.println("A");
} else {
if (score >= 70) { // narrows down further within the else branch
System.out.println("B");
} else {
System.out.println("C");
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
if (cond)के ठीक बाद एक semicolon रखना, इसलिए इसके बाद आने वालेelseका कोई matchingifनहीं और code compile नहीं होता।- Braces छोड़ना और
ifयाelseके नीचे एक दूसरी line जोड़ना, जो हमेशा चलती है और block का हिस्सा नहीं है। if (flag == true) ... elsetest करना जबif (flag)simpler हो।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: