If Else If Ladder

What is the if-else-if Ladder statement in Java?

The if-else-if ladder statement selects a single condition from a collection of statements to execute. The if statements are executed from the top down. It checks the conditions to determine if they are true or not. If any condition is true, it executes the corresponding statement. When no conditions hold true, the program gracefully shifts to the “else” block, executing its designated statement in this scenario.

This means if the first ‘if condition’ fails, then we can check the conditions mentioned in the upcoming ‘else-if conditions‘.

Syntax:

if (condition1) {

// block of code to be executed if the condition is true

}

else if (condition2){

// block of code to be executed if the condition is true

}

else {

// specify default code when all conditions are false

}

Explanation:

The code snippet represents an “if-else-ifladder, a structured way of making decisions based on multiple conditions. Here’s an explanation of how it works:

  1. The program first checks the condition1. If it evaluates to true, the code block inside the first “if” statement is executed. This block contains the instructions or actions that should be performed when condition1 is true.
  2. If condition1 happens to be false, the program proceeds to the following “else if” statement, examining the condition2. If condition2 is true, the code block inside the second “if” statement is executed. Contained within this block are the specific instructions or actions designated for execution when condition2 is verified as true.
  3. The program continues evaluating subsequent “else if” conditions in the same manner, one after another, until it finds a condition that is true. When a true condition is found, the corresponding code block is executed, and the program exits the entire “if-else-if” ladder.
  4. If none of the “if” or “else if” conditions are true, the program enters the “else” block (if it exists) and executes the code inside it. The “else” block serves as a default option, containing instructions or actions that should be taken when all preceding conditions in the ladder are false.

Java if-else-if Ladder Statement Example

Example 1:

package com.example.grading; // Package name

public class IFElseLadder {
    public static void main(String[] args) {
        int marks;
        marks = 61;

        if (marks < 40)
            System.out.println("fail");
        else if (marks >= 40 && marks < 50)
            System.out.println("grade D");
        else if (marks >= 50 && marks < 60)
            System.out.println("grade c");
        else if (marks >= 60 && marks < 70)
            System.out.println("grade B");
        else if (marks >= 70 && marks <= 100)
            System.out.println("grade A");
        else
            System.out.println("Don't Know");
    }
}
Output:
grade B

Explanation:

The given Java code determines the grade based on the value of the “marks” variable, which represents the marks obtained by a student in an exam. Here’s how it works:

  1. The value of the “marks” variable is checked against different ranges to determine the corresponding grade:
  2. If “marks” is less than 40, the student is considered to have failed, and the code will print “fail.”
  3. If “marks” is greater than or equal to 40 and less than 50, the student will be assigned a “grade D,” and the code will print “grade D.”
  4. If “marks” is greater than or equal to 50 and less than 60, the student will be assigned a “grade C,” and the code will print “grade C.”
  5. If “marks” is greater than or equal to 60 and less than 70, the student will be assigned a “grade B,” and the code will print “grade B.”
  6. If “marks” is greater than or equal to 70 and less than or equal to 100, the student will be assigned a “grade A,” and the code will print “grade A.”
  7. If none of the above conditions are met, it means that the “marks” value is not within the specified range (less than 40 or greater than 100), and the code will print “Don’t Know.”

In summary, the code uses an “if-else-if” ladder to evaluate the value of the “marks” variable and determine the appropriate grade or result for the student based on their performance in the exam.

Example 2:

package com.example;

public class IfElseIfLadderStatementExample {
    public static void main(String args[]) {
        int month = 8; 
        String season;
        if (month == 12 || month == 1 || month == 2)
            season = "Winter";
        else if (month == 3 || month == 4 || month == 5)
            season = "Spring";
        else if (month == 6 || month == 7 || month == 8)
            season = "Summer";
        else if (month == 9 || month == 10 || month == 11)
            season = "Autumn";
        else
            season = "Invalid Month";
        System.out.println("Name of the season " + season + ".");
    }
}
Output:
Name of the season Summer

Explanation:

  1. The code initializes the variable “month” with a value of 8, representing the month of August.
  2. The code then checks the value of the “month” variable using the “if-else-if”
  3. It first checks if “month” is equal to 12 or 1 or 2. If any of these conditions are true, it means the month falls within the winter In that case, the variable “season” is assigned the value “Winter.”
  4. If the first condition is false, the code proceeds to the next “else if” It checks if “month” is equal to 3 or 4 or 5. If any of these conditions are true, it means the month falls within the spring season. In that case, the variable “season” is assigned the value “Spring.”
  5. If the second condition is false, the code moves to the next “else if” It checks if “month” is equal to 6 or 7 or 8. If any of these conditions are true, it means the month falls within the summer season. In that case, the variable “season” is assigned the value “Summer.”
  6. If the third condition is false, the code proceeds to the last “else if” It checks if “month” is equal to 9 or 10 or 11. If any of these conditions are true, it means the month falls within the autumn season. In that case, the variable “season” is assigned the value “Autumn.”
  7. If none of the “if” or “else if” conditions are true, it means the value of the “month” variable is not within the specified ranges (1 to 12). In this case, the variable “season” is assigned the value “Invalid Month.”
  8. Finally, the program prints the name of the determined season using the value stored in the “season”

In summary, the code determines the season based on the value of the “month” variable and then prints the name of the corresponding season. If the value of “month” does not fall within the range of valid months (1 to 12), it will print “Invalid Month.”

Visited 1 times, 1 visit(s) today

Comments are closed.

Close