![]() |
VOOZH | about |
Nested if in Java refers to placing one if statement inside another if statement. It is used when a condition depends on another condition being true before it can be evaluated. This helps in handling multiple related decisions in a structured manner.
The above diagram shows that the inner if condition is checked only after the outer if condition evaluates to true. If any condition evaluates to false, the corresponding block is skipped and program control moves to the next statement.
GeeksforGeeks
Explaination: In the above example, one if condition is placed inside another. If both conditions are true, it prints GeeksforGeeks
if (condition1) {
if (condition2) {
if (condition3) {
// Statements
}
}
}
Note: If the outer condition satisfies then only the inner condition will be checked. Along with if condition, else condition can also be executed.
Example 2: The below Java program demonstrates the use of nested if-else statements to execute multiple conditions and different code block based on weather the inner condition is true or false.
GFG
Explanation: In the above example, it first checks if a is equal to 10. If the condition satisfies, it then checks the inner condition b != 20. If the inner condition is false, the else block executes, it prints GFG.