codegurukool.com · Free coding tutorials & online compiler

Home › Java Tutorial › Java if...else

Java if...else

Make decisions.

Use if, else if and else. The condition goes in brackets and must be a boolean. A short form is the ternary operator: condition ? a : b.

Example code

public class Main {
  public static void main(String[] args) {
    int score = 72;
    if (score >= 90) {
      System.out.println("A");
    } else if (score >= 70) {
      System.out.println("B");
    } else {
      System.out.println("C");
    }
    String r = score >= 60 ? "pass" : "fail";
    System.out.println(r);
  }
}

Run this code in the online editor

← Java booleansJava switch →