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

Java का if-else Statement

if-else road में एक fork जैसा है: अगर condition true है आप एक path लेते हैं, और अन्यथा आप दूसरा लेते हैं। दोनों में से एक हमेशा होता है।
Syntax
java
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

java
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
		}
	}
}

Conditional Assignments

इसे भरने के लिए एक empty variable के बाद एक अलग if-else block के बजाय, आप हर branch के अंदर सीधे एक value compute और assign कर सकते हैं, decision और assignment को visually साथ रखते हुए।

उदाहरण: Conditional Assignments

java
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);
	}
}

Checking Boolean Toggles

Condition के रूप में एक single boolean variable, जैसे if (isPremium) {...} else {...}, लगभग plain English जैसा पढ़ता है और एक feature flag या account status के आधार पर behavior branch करने का सबसे clean तरीका है।

उदाहरण: Checking Boolean Toggles

java
// 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");
		}
	}
}

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

java
// 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");
		}
	}
}

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

java
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");
			}
		}
	}
}
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. if (cond) के ठीक बाद एक semicolon रखना, इसलिए इसके बाद आने वाले else का कोई matching if नहीं और code compile नहीं होता।
  2. Braces छोड़ना और if या else के नीचे एक दूसरी line जोड़ना, जो हमेशा चलती है और block का हिस्सा नहीं है।
  3. if (flag == true) ... else test करना जब if (flag) simpler हो।

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.