Nested if Statements in Java: Syntax and Usage Explained
☰Fullscreen
Table of Content:
Nested if statement in java
Example 1: Nestesd if statement
public class NestedIfExample { public static void main(String[] args) { int i=3, j=2, k=1; if (i > k) { if (j > k) System.out.println("i and j are greater than k"); } else System.out.println("i is less than or equal to k"); } }
Output
i and j are greater than k Press any key to continue . . .
Example 2: Nestesd if statement
public class NestedIfExample { public static void main(String[] args) { int a = 3; int b = 1; if( a == 3 ) { if( b == 1 ) // this is nested if { System.out.println("a = 3 and b = 1"); } } else { System.out.println("a != 3 and b != 1"); } } }
Output
a = 3 and b = 1 Press any key to continue . . .
Example 3: Nestesd if statement
public class NestedIfExample { public static void main(String[] args) { int a = 4; int b = 1; if( a == 3 ) { if( b == 1 ) // this is nested if { System.out.println("a = 3 and b = 1"); } } else { if( b == 1 ) // this is nested if { System.out.println("a != 3 and b = 1"); } } } }
Output
a != 3 and b = 1 Press any key to continue . . .