Java if-else if-else Conditional Statement

The Java if-else if-else statement is a conditional statement that allows a program to execute different blocks of code based on multiple conditions. The basic syntax of an if-else if-else statement is as follows:

if (condition1) {
  // code to execute if condition1 is true
} else if (condition2) {
  // code to execute if condition1 is false and condition2 is true
} else {
  // code to execute if both condition1 and condition2 are false
}

Here, the condition1 and condition2 are boolean expressions that evaluate to either true or false. If condition1 is true, the block of code inside the first set of braces is executed. If condition1 is false and condition2 is true, the block of code inside the second set of braces is executed. If both condition1 and condition2 are false, the block of code inside the third set of braces is executed.

For example, consider the following code that determines the letter grade for a given percentage score:

int score = 85;

if (score >= 90) {
  System.out.println("A");
} else if (score >= 80) {
  System.out.println("B");
} else if (score >= 70) {
  System.out.println("C");
} else if (score >= 60) {
  System.out.println("D");
} else {
  System.out.println("F");
}

In this code, the first condition score >= 90 checks whether the score is greater than or equal to 90. If it is, the program prints “A”. If it is not, the second condition score >= 80 checks whether the score is greater than or equal to 80. If it is, the program prints “B”. This process continues with the remaining conditions, with the last condition being the catch-all for any scores below 60, in which case the program prints “F”.

It’s important to note that if-else if-else statements are evaluated from top to bottom, and the first condition that evaluates to true is the block of code that will be executed. This means that if a condition that evaluates to true appears earlier in the code, any subsequent conditions will not be evaluated.

For example, consider the following code:

int x = 10;

if (x > 5) {
  System.out.println("x is greater than 5");
} else if (x > 8) {
  System.out.println("x is greater than 8");
} else {
  System.out.println("x is less than or equal to 5");
}

In this code, the first condition x > 5 is true, so the program prints “x is greater than 5”. The second condition x > 8 is not evaluated, even though it is also true, because the first condition was already true.

Complete code example:

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter your age: ");
    int age = scanner.nextInt();

    if (age < 0) {
      System.out.println("Age cannot be negative.");
    } else if (age < 18) {
      System.out.println("You are a minor.");
    } else if (age < 65) {
      System.out.println("You are an adult.");
    } else {
      System.out.println("You are a senior citizen.");
    }
  }
}

In this example, the program prompts the user to enter their age using the Scanner class from the java.util package. It then uses an if-else if-else statement to check the age of the user and print a message based on their age.

The first condition age < 0 checks whether the age entered by the user is negative. If it is, the program prints a message saying that age cannot be negative. The second condition age < 18 checks whether the age is less than 18. If it is, the program prints a message saying that the user is a minor. The third condition age < 65 checks whether the age is less than 65. If it is, the program prints a message saying that the user is an adult. The final else block is executed if none of the previous conditions were true, and it prints a message saying that the user is a senior citizen.

You can run this code and test it by entering different ages. The program will print a different message depending on the age entered by the user.

Also, see the example code JavaExamples_NoteArena in our GitHub repository. See complete examples in our GitHub repositories.

Follow us on social media
Follow Author