Skip to main content

If-else-if Ladder Statement in Java

  • An if-else if ladder is a series of if and else if statements that allows you to check multiple conditions.

  • It is useful when you need to choose between more than two options based on different conditions.

  • This control flow structure allows your program to evaluate multiple conditions in sequence and execute the corresponding block of code for the first true condition.

Syntax of an if-else if Ladder

Here is the basic syntax of an if-else if ladder in Java:

if (condition1) {
// code to be executed if condition1 is true
} else if (condition2) {
// code to be executed if condition2 is true
} else if (condition3) {
// code to be executed if condition3 is true
} else {
// code to be executed if all conditions are false
}
  • condition1, condition2, condition3, ...: Boolean expressions that evaluate to either true or false.

  • code blocks: Sets of statements that will be executed if the corresponding condition is true.

  • else block: The set of statements that will be executed if none of the conditions are true.

How It Works

  • When an if-else if ladder is encountered, the conditions are evaluated from top to bottom:

  • If condition1 is true, the code block inside the first set of curly braces {} is executed, and the rest of the ladder is skipped.

  • If condition1 is false, the next condition condition2 is evaluated.

  • This process continues until a true condition is found or until all conditions have been evaluated.

  • If none of the conditions are true, the code block inside the else set of curly braces {} is executed.

Example of an if-else if Ladder

Let's look at a simple example to understand how an if-else if ladder works. Consider the following code:

public class Main {
public static void main(String[] args) {
int number = 0;

if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}

Explanation:

  • Declaration of the variable: We declare an integer variable number and assign it a value of 0.

  • The if statement: We check if number is greater than 0 using the condition number > 0.

  • Code block (if): If number is greater than 0, the code inside the first set of curly braces {} will execute, printing "The number is positive."

  • The else if statement: If the first condition is false, we check if number is less than 0 using the condition number < 0.

  • Code block (else if): If number is less than 0, the code inside the second set of curly braces {} will execute, printing "The number is negative."

  • The else statement: If none of the above conditions are true, the code inside the else set of curly braces {} will execute, printing "The number is zero."

Since number is 0 (which is neither greater than 0 nor less than 0), the first two conditions are false, and the else block will execute.

The output will be:

The number is zero.

Summary

  • if-else if ladder: Evaluates multiple conditions in sequence and executes the corresponding block of code for the first true condition.

  • Conditions: Boolean expressions that determine which code block will be executed.

  • Code blocks: The statements that run depending on which condition is true, enclosed in curly braces {}.

  • else block: The set of statements that run if none of the conditions are true.

The if-else if ladder is a versatile tool for handling multiple conditions in your Java programs.