How to Use Else Statement in Java


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

Source Code

package com.beginner.examples;

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

Output:

2

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments