Java if else ladder statement

Description

The if else ladder statement is used to work on multiple conditions.

Syntax

The if-else-if Ladder looks like this:

 
if(condition) // w ww .  j ava  2s  .co m
   statement; 
else if(condition) 
   statement; 
else if(condition) 
   statement; 
. 
.
else 
   statement;

Example

Here is a program that uses an if-else-if ladder.

 
public class Main {
  public static void main(String args[]) {
    int month = 4;
    String value;//  w  w  w.j a  v a  2s .  c  o  m
    if (month == 1 )
      value = "A";
    else if (month == 2)
      value = "B";
    else if (month == 3)
      value = "C";
    else if (month == 4)
      value = "D";
    else
      value = "Error";

    System.out.println("value = " + value);
  }
}

Here is the output produced by the program:





















Home »
  Java Tutorial »
    Java Language »




Java Data Type, Operator
Java Statement
Java Class
Java Array
Java Exception Handling
Java Annotations
Java Generics
Java Data Structures