Java Nested If-else Statements

85
views
Java Nested If-else Statements

In this tutorial, you’ll learn about Java Nested If-else Statements and how to use it in Java Programs.

It is possible to include if-else block inside the if block. It is called nested if-else statements, In this case, the inner if the condition is checked only when the outer if the condition is true.

Java Nested If-else Statements flowchart
Java Nested If-else Statements

In the given example, we check for the largest number of the three numbers.

public class Largest{
	public static void main(String []args){
		int num1=10,num2=20,num3=5;
		if(num1>num2){
			if (num1>num3) {
				System.out.println("Largest Number is:"+num1);
			}
		}else if(num2>num3){
			System.out.println("Largest Number is:"+num2);
		}else{
			System.out.println("Largest Number is:"+num3);
		}
	}
}

Output

Largest Number is:20

Ask your questions about Java control flow if else Ladder statement and clarify your/others doubts by commenting. Documentation.

Please write to us at [email protected] to report any issue with the above content or for feedback.

This tutorial is contributed by Ashutosh Sahu

PREVIOUS
JAVA IF..ELSE LADDER STATEMENT
NEXT
JAVA SWITCH STATEMENT