Multiple conditions can be evaluated successively using the Java if-else-if ladder. It enables a program to run the block of code linked to the first true condition after checking many conditions. As a backup, an optional else block may run if none of the circumstances are met.
Example: The below example demonstrates a straightforward if-else-if ladder structure. It evaluates a number and prints the corresponding message based on the condition.// Java program to demonstrate a simple if-else-if ladder
class GFG {
public static void main(String[] args) {
int i = 20;
// if-else-if ladder to check the value of i
if (i == 10) {
System.out.println("i is 10");
} else if (i == 20) {
System.out.println("i is 20");
} else {
System.out.println("i is neither 10 nor 20");
}
}
}
Output
i is 20
Dry-Running Above Example
- Program starts.
i
is initialized to20
.- The first
if
conditioni == 10
is checked:- It evaluates to
false
becausei
is20
.
- It evaluates to
- The second
else if
conditioni == 20
is checked:- It evaluates to
true
becausei
is20
. - 4.a)
"i is 20"
gets printed.
- It evaluates to
- The
else
block is skipped since a condition was satisfied. - Program ends.

Syntax of if-else-if ladder:
if (condition)
statement 1;
else if (condition)
statement 2;
.
.
else
statement;
Working of the if-else-if ladder:
- The if block is in charge of control.
- Condition 1 is where the flow leaps to.
- The state is examined.
- Proceed to Step 4 if Condition returns true.
- Proceed to Step 5 if Condition returns false.
- The current block is run. Proceed to Step 7.
- Condition 2 is where the flow changes to.
- Proceed to step 4 if Condition returns true.
- Proceed to Step 6 if Condition returns false.
- Condition 3 is where the flow changes to.
- Proceed to step 4 if Condition returns true.
- Otherwise, run the other block if Condition returns false.
- Proceed to Step 7.
- Get out of the if-else-if hierarchy.
Flowchart if-else-if ladder:
The operation of an if-else-if ladder in a program is depicted in the flowchart above. It examines the conditions one after the other, and if none of them are true, it either runs the corresponding statement or the else block.
// Java program to illustrate if-else-if ladder
import java.io.*;
class GFG {
public static void main(String[] args) {
// Initializing expression
int i = 20;
// Checking conditions using if-else-if ladder
if (i == 10) {
System.out.println("i is 10");
} else if (i == 15) {
System.out.println("i is 15");
} else if (i == 20) {
System.out.println("i is 20");
} else {
System.out.println("i is not present");
}
// Statement outside if-else-if ladder
System.out.println("Outside if-else-if");
}
Output
i is 20 Outside if-else-if
Explanation: The above example demonstrates an if-else-if ladder to check the value of “i"
against multiple conditions. It prints the matching condition’s output or a default message.
Advantages of Java if-else-if Ladder
- Sequential condition checking is helpful for managing a variety of scenarios since it enables the evaluation of several criteria in order.
- Readability: For basic decision-making logic, it is simple to read and comprehend.
- Fallback Mechanism: In the event that none of the conditions are met, this offers an optional else block.
- Versatile: This is applicable to logical and numerical comparisons.
- Simpler Alternative: It works well in circumstances like complex settings when utilizing a switch is impractical.
Do all ABAPers know Fixed Point Arithmetic?
Use of data elements in SAP ABAP
C++ Programming Course Online – Complete Beginner to Advanced