Java if-else-if ladder with Examples

01 Apr, 2022
Decision Making in Java helps to write decision driven statements and execute a particular set of code based on certain conditions.Java if-else-if ladder is used to decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed. 
Syntax:  
if (condition)
    statement 1;
else if (condition)
    statement 2;
.
.
else
    statement;
Working of the if-else-if ladder:  
Control falls into the if block.The flow jumps to Condition 1.Condition is tested. If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.The present block is executed. Goto Step 7.The flow jumps to Condition 2. If Condition yields true, goto step 4.If Condition yields false, goto Step 6.The flow jumps to Condition 3. If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.Exit the if-else-if ladder.
Control falls into the if block.
The flow jumps to Condition 1.
Condition is tested. If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.If Condition yields false, goto Step 5.
If Condition yields true, goto Step 4.
If Condition yields false, goto Step 5.
The present block is executed. Goto Step 7.
The flow jumps to Condition 2. If Condition yields true, goto step 4.If Condition yields false, goto Step 6.
If Condition yields true, goto step 4.If Condition yields false, goto Step 6.
If Condition yields true, goto step 4.
If Condition yields false, goto Step 6.
The flow jumps to Condition 3. If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.
If Condition yields true, goto step 4.If Condition yields false, execute else block. Goto Step 7.
If Condition yields true, goto step 4.
If Condition yields false, execute else block. Goto Step 7.
Exit the if-else-if ladder.
Flowchart if-else-if ladder:  
Example 1: 
Java
// Java program to illustrate if-else-if ladder import java.io.*; class GFG {    public static void main(String[] args)    {        // initializing expression        int i = 20;         // condition 1        if (i == 10)            System.out.println("i is 10\n");         // condition 2        else if (i == 15)            System.out.println("i is 15\n");         // condition 3        else if (i == 20)            System.out.println("i is 20\n");         else            System.out.println("i is not present\n");         System.out.println("Outside if-else-if");    }}
i is 20

Outside if-else-if
 
Time Complexity: O(1)
Auxiliary Space : O(1)
Dry running Example 1:  
1. Program starts.
2. i is initialized to 20.
3. condition 1 is checked. 20 == 10, yields false.
4. condition 2 is checked. 20 == 15, yields false.
5. condition 3 is checked. 20 == 20, yields true.
  5.a) "i is 20" gets printed.
6. "Outside if-else-if" gets printed.
7. Program ends.
Example 2:  
Java
// Java program to illustrate if-else-if ladder import java.io.*; class GFG {    public static void main(String[] args)    {         // initializing expression        int i = 20;         // condition 1        if (i < 10)            System.out.println("i is less than 10\n");         // condition 2        else if (i < 15)            System.out.println("i is less than 15\n");         // condition 3        else if (i < 20)            System.out.println("i is less than 20\n");         else            System.out.println("i is greater than "                               + "or equal to 20\n");         System.out.println("Outside if-else-if");    }}
i is greater than or equal to 20

Outside if-else-if
 
Time Complexity: O(1)
Auxiliary Space : O(1)
Related Articles:  
Decision Making in JavaJava if statement with ExamplesJava if-else statement with ExamplesSwitch Statement in JavaBreak statement in Javareturn keyword in Java
Decision Making in Java
Java if statement with Examples
Java if-else statement with Examples
Switch Statement in Java
Break statement in Java
return keyword in Java
 
rishavnitro
java-basics
Java
School Programming
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Split() String method in Java with examples
Arrays.sort() in Java with examples
Reverse a string in Java
How to iterate any Map in Java
Stream In Java
Python Dictionary
Reverse a string in Java
Arrays in C/C++
Introduction To PYTHON
Inheritance in C++