WeCodeAcademy

Java If...Else

Use if, else if and else to choose between paths.

ExampleJava
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");
    }

    // Short-hand: the ternary operator
    String result = (score >= 50) ? "pass" : "fail";
    System.out.println(result);
  }
}
Output
B
pass
Quick check

Which keyword runs code when the if condition is false?

Finish the quick check (and any code exercise) to complete this lesson, or mark it done yourself.

Tip: use the left and right arrow keys to move between lessons.