In this tutorial you will learn about the Java Nested If Else Statement and its application with practical example.
Java Nested if-else Statement
In Java, when there is an if statement inside another if statement then it is known as nested if-else. Nested if-else can also be simplified using Java Switch Case Statement.
Syntax:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition1) { if(condition2){ // statements } else { // statements } } else { if(condition3){ // statements } else { // statements } } |
Example:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
public class Main { public static void main(String[] args) { int a = 25; System.out.println("W3Adda - Java Nested If Statement"); if(a < 100){ if(a < 50){ System.out.println("a is less than 50"); } if(a >= 50){ System.out.println("a is greater than 50"); } } } } |
When we run the above Java program, will see following output –
Output:-