Tutorials

Java Tutorial

All Tutorials Learning Tools Login

Java Tutorial · Chapter 7 of 12

Java If Else

Make decisions.

if/else selects different actions based on a condition.

Use nested if when one decision depends on another.

If sample Try it Yourself »
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter score: ");
        int score = input.nextInt();
        if (score >= 75) {
            System.out.println("Passed");
        } else {
            System.out.println("Failed");
        }
    }
}

Classroom Tips

  • Use == for equality of primitives.
  • For String content, prefer equals().