Nested-if statement allows the user to use if block inside the other if block. And the inner if is executed only if the outer if is true in condition.
Flowchart for nested-if statement in Java:
Syntax of nested if statement:
1 2 3 4 5 6 7 8 | if(condition1) { //block of code to be executed if(condition2) { //block of code to be executed } } |
Example of a nested-if statement in Java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | public class NestedIfTest { public static void main(String args[]) { int num1 = 20; int num2 = 30; if( num1 >= 10 ) { if( num2 >= 15 ) { System.out.println("a and b both are true"); } System.out.println("code inside the first if statement"); } } } |
Output:
1 2 | a and b both are true code inside the first if statement |