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

Java का if Statement

एक if statement कुछ code सिर्फ तब चलाता है जब एक condition true हो, सिर्फ ठंड होने पर coat लेने जैसा। अगर condition false है, Java इसे skip करता है।
Syntax
java
if (condition) {
    // code runs if condition is true
}

Basic if Statement

एक if statement अपनी parenthesized boolean condition एक बार evaluate करता है; अगर यह true है, इसके बाद वाला block execute होता है, और अगर यह false है, Java उस block से सीधे आगे जो भी code आता है उस पर skip करता है।

उदाहरण: Basic if Statement

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 20
		int age = 20;
		// Check whether age >= 18
		if (age >= 18) {
			// Print a line to the console
			System.out.println("Condition was true - block executed");
		}
	}
}

Comparisons inside if Conditions

एक condition के अंदर Double equals (==) जांचता है कि दो primitive values बिल्कुल match करती हैं या नहीं -- एक बहुत आम beginner mistake यहां एक single = लिखना है, जो assignment है, comparison नहीं, और आमतौर पर boolean contexts के लिए एक compiler error का कारण बनता है।

उदाहरण: Comparisons inside if Conditions

java
public class Main {
	public static void main(String[] args) {
		int x = 5;
		if (x == 5) { // == is comparison; a single '=' here would be a compile error
			System.out.println("x equals 5");
		}
	}
}

Logical AND (&&) inside if

एक if के अंदर && के साथ conditions chain करना मतलब है block execute होने के लिए हर एक को true evaluate होना चाहिए -- अगर कोई single condition false है, पूरी check तुरंत false पर short-circuit हो जाती है।

उदाहरण: Logical AND (&&) inside if

java
public class Main {
	public static void main(String[] args) {
		int age = 25;
		boolean hasId = true;
		if (age >= 18 && hasId) { // both must be true
			System.out.println("Entry allowed");
		}
	}
}

Logical OR (||) inside if

इसके बजाय || उपयोग करना block को चलने देता है अगर कई conditions में से सिर्फ एक holds करती है, जो तब सही choice है जब आप कई acceptable situations में से किसी भी एक पर react करना चाहें।

उदाहरण: Logical OR (||) inside if

java
public class Main {
	public static void main(String[] args) {
		boolean isAdmin = false;
		boolean isOwner = true;
		if (isAdmin || isOwner) { // runs if just one holds
			System.out.println("Access granted");
		}
	}
}

Nested if Statements

किसी if statement को किसी दूसरे if के block के अंदर रखना आपको एक दूसरा, ज़्यादा specific सवाल सिर्फ तब पूछने देता है जब पहली, broader condition पहले से confirmed true हो -- layered eligibility checks में drill down करने के लिए उपयोगी।

उदाहरण: Nested if Statements

java
public class Main {
	public static void main(String[] args) {
		int age = 20;
		boolean hasLicense = true;
		if (age >= 18) {
			if (hasLicense) { // more specific question, asked only after the first passes
				System.out.println("Can drive");
			}
		}
	}
}
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. Condition के बाद एक semicolon रखना, जैसे if (x > 5);, जो body को हमेशा चलाता है।
  2. Curly braces skip करना और एक दूसरा statement जोड़ना जो हमेशा चलता है।
  3. if (x = 5) लिखना, जो non-boolean values के लिए compile नहीं होता।

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.