Loading...

Nested if-else statement in Java (with examples)

Nested if-else statement in Java (with examples)

When faced with a choice, we always ask ourselves a series of questions before determining the best course of action in light of the circumstances. In programming languages, we implement this very case with the use of if-else statements. But, in some cases, one condition is not enough to get the desired answer. In such cases, we implement if else statements one inside another, this is known as nested if else. In this article, we’ll learn how to implement nested if-else statement in java.

If else

Before moving to nested if-else statements. Let’s revise what if else statements are. An if-else statement is a conditional statement that decides the execution path based on whether the condition is true or false. In other words, an if-else statement is used to carry out a particular task (job) based on whether or not a defined condition is true.

if ( condition ) { statement1; // For true case. } else { statement2; // For false case. }
Code language: Java (java)

Each case in this example contains either a single statement or many statements enclosed in curly braces indicating an action to be performed. Any boolean expression that produces a boolean value qualifies as the condition.

The else clause is optional, i.e it’s not necessary, we can omit the else clause. This convention can be used in all Java control statements. Let’s take a look at some examples.

Example 1: Check whether the number is divisible by 5.

public class codedamn { public static void main(String[] args) { int a=10; if(a%5==0) { //Divisible by 5 System.out.println("Divisibe by 5"); } else { //Not divisible by 5 System.out.println("Not dividible by 5"); } } }
Code language: Java (java)
Output

In the above example, if the if-condition is satisfied, the statement inside it is executed otherwise it moves to the else part. The number a=10 is divisible by 5, so it prints “Divisible by 5” and the else statement is skipped.

Nested if else statements

We saw how helpful if and else statements are, but what if we need to check for more conditions even when one condition is satisfied? In such cases, we use nested if-else statement. Nesting is the practice of enclosing several if-else statements within an if-and-else statement.

Example: Check number divisibility

Let’s take the example of odd and even. If a number is even, we also need to check whether the number is divisible by 6. And if the number is odd, we need to check whether the number is divisible by 3. Here, a single if-else statement won’t be able to solve the problem. Therefore, we need to use nested if-else statements.

public class code{ public static void main(String[] args) { int n=24; //if else condition to check whether the number is even or odd if (n % 2 == 0){ //the number is even System.out.print("Even "); //nested if else condition to check if n is divisible by 6 or not if (n % 6 == 0) { //the number is divisible by 6 System.out.println("and divisible by 6"); } else { //the number is not divisible by 6 System.out.println("and not divisible by 6"); } } else { //the number is odd System.out.println("Odd "); //nested if else condition to check if n is divisible by 3 or not if(n % 3 == 0) { //the number is divisible by 3 System.out.println("and divisible by 3"); } else { //the number is not divisible by 3 System.out.println("and not divisible by 3"); } } } }
Code language: Java (java)
Output

First, we’ll use an if statement to determine whether the number is even or odd. Then, if the number were even, we would need to put another if and else statement. In the if block we again check whether the number is divisible by 6 or not. Likewise, in the else block, we would need to determine if the number is divisible by 3 or not.

Example: Find the tallest student among 3 students

public class code { public static void main(String[] args) { int n1 = 150, n2 = 180, n3 = 170; if (n1 >= n2) { if (n1 >= n3) System.out.println("Student with height: " + n1 + " is the tallest."); else System.out.println("Student with height: " + n3 + " is the tallest."); } else { if (n2 >= n3) System.out.println("Student with height: " + n2 + " is the tallest."); else System.out.println("Student with height: " + n3 + " is the tallest."); } System.out.println("\n"); } }
Code language: Java (java)

In the above program, instead of checking for two conditions in a single if statement, we use nested if to find the tallest student’s height. We use the following conditions:

  • If n1 is greater or equal to n2:

if: n1 is greater or equal to n3, n1 is the greatest.

else: n3 is the greatest.

  • Else (i.e n2>n1):

if: n2 is greater or equal to both n3, n2 is the greatest.

else: n3 is the greatest.

Example: Check if three numbers are equal or not

public class code { public static void main(String[] args) { int a = 2; int b = 2; int c = 2; if (a == b) { // nested if else condition to check if c is equal to a and b if (a == c) { // all are equal System.out.println("Equal"); } else { // a!=c System.out.println("Not equal"); } } else { // a!=b System.out.println("Not equal"); } } }
Code language: Java (java)

Here a, b, and c have a value equal to 2. If the condition (a==b) is satisfied, it moves to the next if statement and checks if (a==c). Since this condition is satisfied in the above case it prints “Equal”.

Conclusion

In conclusion, we can say if-else statements for decision-making. They are one of the core programming concepts that you learn when you start programming. Sometimes, we need to check more than one condition, there we implement nested if-else statements. In this article, we went through some examples using nested if-else statements in Java. Additionally, you can start learning Java from scratch in the Codedamn platform by clicking here.

Sharing is caring

Did you like what Fidal Mathew wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far