Java Tutorial Java Advanced Java References

Java else if Keyword



For adding more conditions in if-else statement, else if statement in used. The program first checks if condition. When found false, then it checks else if conditions. If found false, then else code block is executed.

Syntax

if(condition){
  statements;
}
else if(condition){
  statements;
}
...
...
...
else {
  statements;
}
}

Flow Diagram:

Java If-else if-else Loop

In the example below, else if statement is used to add more condition in a if-else statement.

public class MyClass {
  public static void main(String[] args) {
    int i = 16;
    if (i > 25){
      System.out.println(i+" is greater than 25."); 
    } else if(i <=25 && i >=10){
      System.out.println(i+" lies between 10 and 25."); 
    } else {
      System.out.println(i+" is less than 10.");
    }
  }
}

The output of the above code will be:

16 lies between 10 and 25.

❮ Java Keywords