A nested if statement means placing one if
statement inside another if statement.
It is useful when a program must check multiple related
conditions step by step.
In a nested if statement, the inner condition is checked
only when execution reaches the inner if.
Commonly, this happens after the outer condition is true.
Syntax:
if (condition1) {
if (condition2) {
// Code executes when both conditions are true
}
}
Example:
public class Main {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
if (age >= 18) {
System.out.println("You are an adult.");
}
}
}
}
You are an adult.
Both conditions are true, so the message is displayed.
We can check two different conditions using nested
if statements.
public class Main {
public static void main(String[] args) {
int age = 22;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
}
}
}
}
Entry allowed.
The outer condition checks the age. If it is true, the inner condition checks whether the person has an ID.
You can use else inside a nested
if to handle a false inner condition.
public class Main {
public static void main(String[] args) {
int age = 20;
boolean hasID = false;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
} else {
System.out.println("Please show your ID.");
}
} else {
System.out.println("You are too young.");
}
}
}
Please show your ID.
The outer condition is true, but the inner condition
is false. Therefore, the inner else runs.
A nested if can check whether a student has passed and then determine the grade.
public class Main {
public static void main(String[] args) {
int marks = 85;
if (marks >= 40) {
System.out.println("You have passed.");
if (marks >= 80) {
System.out.println("Grade A");
} else {
System.out.println("Grade B");
}
} else {
System.out.println("You have failed.");
}
}
}
You have passed.
Grade A
First, Java checks whether marks are at least 40. If the student passes, Java checks the grade.
Nested if statements can check whether a number is positive and whether it is even.
public class Main {
public static void main(String[] args) {
int number = 12;
if (number > 0) {
System.out.println("Positive number");
if (number % 2 == 0) {
System.out.println("Even number");
} else {
System.out.println("Odd number");
}
} else {
System.out.println("Number is not positive.");
}
}
}
Positive number
Even number
The even-or-odd condition is checked only when the number is positive.
You can place multiple nested if statements inside one another when each check depends on the previous condition.
public class Main {
public static void main(String[] args) {
int age = 25;
boolean hasLicense = true;
boolean hasVehicle = true;
if (age >= 18) {
if (hasLicense) {
if (hasVehicle) {
System.out.println("Ready to drive.");
}
}
}
}
}
Ready to drive.
All three conditions must be true for the message to appear in this example.
Each nested if statement can have its own
else block.
public class Main {
public static void main(String[] args) {
int age = 17;
boolean hasID = true;
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
} else {
System.out.println("ID required.");
}
} else {
System.out.println("Age requirement not met.");
}
}
}
Age requirement not met.
Because the outer condition is false, Java skips the entire inner if-else block.
Nested if statements can compare three numbers to determine the largest value.
public class Main {
public static void main(String[] args) {
int a = 25;
int b = 40;
int c = 30;
if (a > b) {
if (a > c) {
System.out.println("A is largest");
} else {
System.out.println("C is largest");
}
} else {
if (b > c) {
System.out.println("B is largest");
} else {
System.out.println("C is largest");
}
}
}
}
B is largest
A nested if statement can represent a simple username-and-password check.
public class Main {
public static void main(String[] args) {
String username = "admin";
String password = "12345";
if (username.equals("admin")) {
if (password.equals("12345")) {
System.out.println("Login successful.");
} else {
System.out.println("Incorrect password.");
}
} else {
System.out.println("Incorrect username.");
}
}
}
Login successful.
This is only a learning example. Real applications should use secure authentication and hashed passwords, not hard-coded credentials.
If both conditions must be true, you can sometimes
replace nested if statements with the logical AND
operator &&.
if (age >= 18) {
if (hasID) {
System.out.println("Entry allowed.");
}
}
if (age >= 18 && hasID) {
System.out.println("Entry allowed.");
}
Both versions produce the same result for these conditions. Nested if statements are especially useful when the inner decision should be made only after the outer condition is satisfied.
Strings can be compared using
equals() inside nested conditions.
public class Main {
public static void main(String[] args) {
String course = "Java";
int duration = 6;
if (course.equals("Java")) {
if (duration >= 6) {
System.out.println("Eligible for the advanced course.");
} else {
System.out.println("Complete the required duration.");
}
} else {
System.out.println("Course not available.");
}
}
}
Eligible for the advanced course.
Use equals() to compare String contents
rather than using ==.
if (age >= 18) {
if (hasID) {
System.out.println("Allowed");
}
}
Make sure each opening brace has a matching closing brace.
// Correct comparison
if (number == 10) {
System.out.println("Number is 10");
}
// Correct way
if (username.equals("admin")) {
System.out.println("Username matched");
}
The inner block will not execute if the outer condition is false, even when the inner condition would otherwise be true.
This example checks a student's age, marks, and entrance-test status before displaying an admission decision.
public class Main {
public static void main(String[] args) {
int age = 19;
int marks = 75;
boolean passedTest = true;
if (age >= 18) {
if (marks >= 60) {
if (passedTest) {
System.out.println("Admission approved.");
} else {
System.out.println("Entrance test required.");
}
} else {
System.out.println("Minimum marks not achieved.");
}
} else {
System.out.println("Minimum age requirement not met.");
}
}
}
Admission approved.
Q1. What is a nested if statement?
Q2. When is the inner if block checked in this example?
if (age >= 18) {
if (hasID) {
System.out.println("Allowed");
}
}
Q3. Which method is recommended for comparing String contents?