Java Tutorial

Java If Else

if runs a block when a condition is true. else and else if cover the other paths.

if and else

if tests a boolean. When it is true, the block runs. else runs when it is not.

Example

public class Main {
  public static void main(String[] args) {
    int n = 7;
    if (n % 2 == 0) System.out.println("even");
    else System.out.println("odd");
  }
}

else if

Chain extra tests with else if. The first true branch wins. Put the default last.

Example

public class Main {
  public static void main(String[] args) {
    int score = 82;
    if (score >= 90) System.out.println("A");
    else if (score >= 80) System.out.println("B");
    else System.out.println("C");
  }
}

Braces keep a block together

A one-line body can skip the braces, but a block with braces is safer. The moment a branch needs a second statement, the braces are what bind the two lines to the if — without them, only the first line belongs to the condition and the second always runs.

Example

public class Main {
  public static void main(String[] args) {
    int temp = 32;
    if (temp <= 0) {
      System.out.println("Freezing");
      System.out.println("Wear a coat");
    } else {
      System.out.println("Above freezing");
    }
  }
}

Combine conditions

Join tests with && (both must be true) and || (either is enough). This is how you ask real questions like "old enough and a member".

Example

public class Main {
  public static void main(String[] args) {
    int age = 20;
    boolean member = true;
    if (age >= 18 && member) {
      System.out.println("Welcome in");
    } else {
      System.out.println("Not yet");
    }
  }
}

Use == to compare, not =. A single = is assignment; writingif (x = 5) is a compile error in Java (it is not a boolean) — the language catches this classic slip for you.

Try It Yourself

Exercise: Read an int hour (0–23) and print "Good morning" before 12, "Good afternoon" before 18, otherwise "Good evening".

Show solution
if (hour < 12) System.out.println("Good morning");
else if (hour < 18) System.out.println("Good afternoon");
else System.out.println("Good evening");

Order matters: because the first true branch wins, checking hour < 12 first means the later tests only see the remaining hours.

Key Takeaways

  • if runs a block when a boolean is true; else covers the rest.
  • Chain with else if; the first true branch wins, so order your tests carefully.
  • Combine conditions with && and ||.
  • Compare with ==, never a single =.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Maths

Pass mark

A threshold is an if. Scores at or above 70 pass. The else covers the rest without a second formula.

Example

public class Main {
  public static void main(String[] args) {
    int score = 71;
    if (score >= 70) System.out.println("pass");
    else System.out.println("retry");
  }
}

FAQ: Java If Else

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java if else examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java if else in this Java Java lesson (Java If Else).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.