JavaNested If
Nested If
You can also place anif statement inside anotherif. This is called anested if statement.
A nestedif lets you check for a condition only if another condition is alreadytrue.
Syntax
if (condition1) { // code to run if condition1 is true if (condition2) { // code to run if both condition1 and condition2 are true }}Example
In this example, we first check ifx is greater than 10. If it is, we then check ify is greater than 20:
Example
int x = 15;int y = 25;if (x > 10) { System.out.println("x is greater than 10"); // Nested if if (y > 20) { System.out.println("y is also greater than 20"); }}Result:
x is greater than 10
y is also greater than 20Real-Life Example
Nestedif statements are useful when you need to test multiple conditions that depend on each other. For example, checking if a person is old enough to vote, and if they are a citizen:
Example
int age = 20;boolean isCitizen = true;if (age >= 18) { System.out.println("Old enough to vote."); if (isCitizen) { System.out.println("And you are a citizen, so you can vote!"); } else { System.out.println("But you must be a citizen to vote."); }} else { System.out.println("Not old enough to vote.");}Result:
Old enough to vote.
And you are a citizen, so you can vote!Notes
- You can nest as many
ifstatements as you want, but avoid making the code too deep - it can become hard to read. - Nested
ifis often used together withelseandelse iffor more complex decision making.

