How to Use Else If Statement in Java


In this example we will show you how to use the else if statement in Java.

Source Code

package com.beginner.examples;

public class ElseIf {
  public static void main(String[] args) {
    int i = 5;
    if (i < 1) { // use the else if statement
      System.out.println("1");
    } else if (i < 2) {
      System.out.println("2");
    }  else {
      System.out.println("3");
    }
  }
}

Output:

3
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments