Java का if Statement
In this page:
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
// 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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Comparisons inside if Conditions
एक condition के अंदर Double equals (==) जांचता है कि दो primitive values बिल्कुल match करती हैं या नहीं -- एक बहुत आम beginner mistake यहां एक single = लिखना है, जो assignment है, comparison नहीं, और आमतौर पर boolean contexts के लिए एक compiler error का कारण बनता है।
उदाहरण: Comparisons inside if Conditions
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Logical AND (&&) inside if
एक if के अंदर && के साथ conditions chain करना मतलब है block execute होने के लिए हर एक को true evaluate होना चाहिए -- अगर कोई single condition false है, पूरी check तुरंत false पर short-circuit हो जाती है।
उदाहरण: Logical AND (&&) inside if
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Logical OR (||) inside if
इसके बजाय || उपयोग करना block को चलने देता है अगर कई conditions में से सिर्फ एक holds करती है, जो तब सही choice है जब आप कई acceptable situations में से किसी भी एक पर react करना चाहें।
उदाहरण: Logical OR (||) inside if
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");
}
}
}
Login to try C/C++/Java/PHP code in the editor
Nested if Statements
किसी if statement को किसी दूसरे if के block के अंदर रखना आपको एक दूसरा, ज़्यादा specific सवाल सिर्फ तब पूछने देता है जब पहली, broader condition पहले से confirmed true हो -- layered eligibility checks में drill down करने के लिए उपयोगी।
उदाहरण: Nested if Statements
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");
}
}
}
}
Login to try C/C++/Java/PHP code in the editor
- Condition के बाद एक semicolon रखना, जैसे
if (x > 5);, जो body को हमेशा चलाता है। - Curly braces skip करना और एक दूसरा statement जोड़ना जो हमेशा चलता है।
if (x = 5)लिखना, जो non-boolean values के लिए compile नहीं होता।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: