Java if-else-if ladder with Examples

Java if-else-if ladder with Examples

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

  1. Program starts.
  2. i is initialized to 20.
  3. The first if condition i == 10 is checked:
    1. It evaluates to false because i is 20.
  4. The second else if condition i == 20 is checked:
    1. It evaluates to true because i is 20.
    2. 4.a) "i is 20" gets printed.
  5. The else block is skipped since a condition was satisfied.
  6. 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:  

  1. The if block is in charge of control.
  2. Condition 1 is where the flow leaps to.
  3. The state is examined.
  4. Proceed to Step 4 if Condition returns true.
  5. Proceed to Step 5 if Condition returns false.
  6. The current block is run. Proceed to Step 7.
  7. Condition 2 is where the flow changes to.
  8. Proceed to step 4 if Condition returns true.
  9. Proceed to Step 6 if Condition returns false.
  10. Condition 3 is where the flow changes to.
  11. Proceed to step 4 if Condition returns true.
  12. Otherwise, run the other block if Condition returns false.
  13. Proceed to Step 7.
  14. 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

admin
admin
https://www.thefullstack.co.in

Leave a Reply