⏱️ 7 min read • Beginner Level • Lesson 24
A nested if statement in Java means writing one
if statement inside another if or else block.
It is used when one condition should be checked only after another condition is true.
Before learning nested if, you should understand Java if statement, Java if-else statement, and Java if-else-if ladder.
A nested if statement is an if statement placed inside another
if statement. The inner condition is checked only when the outer
condition is true.
Nested if is useful when a condition depends on another condition.
The inner if block executes only if the outer condition is true.
Nested if can be used with if, if-else,
and else blocks.
The syntax of nested if contains one if statement inside another
if block.
if (condition1) {
// executes when condition1 is true
if (condition2) {
// executes when both condition1 and condition2 are true
}
}
if condition is checked only when
the outer if condition is true.
Execution flow of a nested if statement
if condition.false, the inner condition is not checked.true, Java enters the outer block.if condition.true, the inner block executes.
The following program checks whether a number is a natural number.
If it is greater than 0, then it checks whether the number is even or odd.
import java.util.Scanner;
public class NestedIfExample {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter any number: ");
int x = sc.nextInt();
if (x > 0) {
if (x % 2 == 0) {
System.out.println("X is EVEN");
} else {
System.out.println("X is ODD");
}
} else {
System.out.println("Not a Natural Number");
}
sc.close();
}
}
Sample Input 1:
12
Output 1:
Enter any number:12 X is EVEN
Sample Input 2:
3
Output 2:
Enter any number:3 X is ODD
Sample Input 3:
-12
Output 3:
Enter any number:-12 Not a Natural Number
Scanner class to read input from the keyboard.Scanner object named sc is created.x.x > 0 checks whether the number is natural.x > 0 is true, the inner condition x % 2 == 0 is checked.X is EVEN.X is ODD.Not a Natural Number.Scanner object is closed using sc.close().This example checks username first. If the username is correct, then it checks the password. This is a good real-life use case of nested if.
public class LoginValidationExample {
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("Invalid Username");
}
}
}
Output:
Login Successful
username as "admin".password as "12345".if checks whether the username is correct.if checks the password.Login Successful.Incorrect Password.Invalid Username.| Nested If | If-Else-If Ladder |
|---|---|
| One condition is checked inside another condition. | Multiple conditions are checked one by one. |
| Useful when one condition depends on another. | Useful when there are multiple independent choices. |
| Can become harder to read if deeply nested. | Usually easier to read for multiple related conditions. |
| Example: login username then password check. | Example: grade calculation based on marks. |
{ }, especially when nesting conditions.= instead of == for comparison.== instead of equals() for comparing strings.if-else-if ladder, or separate methods to make the code cleaner.
if statement inside another.🧠 Test your understanding with a quick quiz
Topic: Nested-if | Language: Java
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("Invalid Username");
}int x = -5;
if (x > 0) {
if (x % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
} else {
System.out.println("Not Natural");
}int x = 12;
if (x > 0) {
if (x % 2 == 0) {
System.out.println("Even");
} else {
System.out.println("Odd");
}
} else {
System.out.println("Not Natural");
}String username = "admin";
if (username.____("admin")) { }🎉 Great job! Continue learning Java step by step.