If – else Ladder Statement in Java.

In java programming, with if-else ladder statements you have choices to decide from multiple options. Here if statements are executed in top down manner. When 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 no if conditions is true, then the final else statement will be executed.

Syntax

 if (condition){
      //Statement to be executed
 }else if (condition) {
      //Statement to be executed
 }else if (condition) {
      //Statement to be executed
 }
 ......
 else {
      //Statement to be executed
 } 

Example

You can find the code here

package com.qacaffe.examples.ControlStatements;

import java.util.Scanner;

public class IfElseIf {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter your test score : ");
        int score = scanner.nextInt();

        char grade;

        if (score >= 90) {
            grade = 'A';
        } else if (score >= 80) {
            grade = 'B';
        } else if (score >= 70) {
            grade = 'C';
        } else if (score >= 60) {
            grade = 'D';
        } else {
            grade = 'F';
        }
        System.out.println("Your grade is " + grade);
    }
}

/*
Output :

Enter your test score :
91
Your grade is A

 */

Explanation

The given program calculates the grade of the score.

Rutu Shah