Java if else ladder statement

In this chapter you will learn:

  1. What is Java if else ladder statement
  2. Syntax for Java if else ladder statement
  3. Example - 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) /*  ww w .  jav a2s.  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;/*from  w ww .jav  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:

Next chapter...

What you will learn in the next chapter:

  1. What is Java nested if statement
  2. Example - Java nested if statement
Home »
  Java Tutorial »
    Java Langauge »
      Java Statement
Java if Statement
Java if else Statement
Java if else ladder statement
Java nested if statement
Java switch Statement
Java for loop
Java for each loop
Java while Loop
Java do while loop
Java break statement
Java continue statement
Java Comments
Java documentation comment(Javadoc)