CORE JAVA

ProwessApps Learning Portal

If-else if Ladder in Java


Useful for if-else kind of conditional execution when we have more than two related conditional statements.

Syntax :


if(boolean_expression 1) {
   /* Executes when the boolean expression 1 is true */
}else if( boolean_expression 2) {
   /* Executes when the boolean expression 2 is true */
}else if( boolean_expression 3) {
   /* Executes when the boolean expression 3 is true */
} else  {
   /* executes when the none of the above condition is true. */
}
                                    

Block Diagram :

prowessapps.com

Explanation:

  • The if keyword is followed by a condition enclosed in parentheses. If this condition evaluates to true, the corresponding code block is executed.

  • If the first if condition is false, the control moves to the next else if block, and its condition is evaluated. If true, its corresponding code block is executed.

  • This process continues for each else if block until a true condition is found or until there are no more else if blocks left.

  • If none of the conditions in the if and else if blocks are true, the code block inside the else block is executed.

  • Only one code block among all the if, else if, and else blocks will be executed.


Example:

Test.java Copy Code
public class Test
{
 public static void main(String []arg) {
  int x = 10;
  if (x > 10) {
        System.out.println("x is greater than 10");
    } 
  else if (x < 10) {
        System.out.println("x is less than 10");
    } 
  else {
        System.out.println("x is equal to 10");
    }
 }
}          

In this example, the output will be "x is equal to 10" because the value of x is 10, so the condition in the else block is true.

Example:

Program to print the grade of student.
GradeExample.java Copy Code
public class GradeExample {
    public static void main(String[] args) {
        int marks = 75;

        if (marks >= 90) {
            System.out.println("Grade A");
        } else if (marks >= 80) {
            System.out.println("Grade B");
        } else if (marks >= 70) {
            System.out.println("Grade C");
        } else if (marks >= 60) {
            System.out.println("Grade D");
        } else {
            System.out.println("Grade F");
        }
    }
}
          

Explanation:

  1. We declare a class named GradeExample.

  2. Inside the class, we define the main method, which is the entry point of the program.

  3. We declare an integer variable marks and initialize it with the value 75.

  4. We use a series of if-else-if statements to determine the grade based on the value of marks :

    • If marks is greater than or equal to 90, it prints "Grade A".

    • Otherwise, if marks is greater than or equal to 80, it prints "Grade B".

    • Otherwise, if marks is greater than or equal to 70, it prints "Grade C".

    • Otherwise, if marks is greater than or equal to 60, it prints "Grade D".

    • Otherwise, it prints "Grade F".

  5. The program terminates after executing the main method.

  6. When you run this program with marks set to 75, the output will be:

     Grade C 

This output confirms that the value of marks falls within the range for "Grade C", so the corresponding message is printed. Depending on the value of marks, different messages will be printed accordingly.





advertisement