1. java-hell.de
  2. Topics
  3. If-Else
Topic 3 of 11

Learn Java If-Else – Practice Conditions

From here on, your program makes decisions. With if, else and else if you react to values, and comparison operators tell you what's what.

The exercises

What you'll learn in this topic

  1. Your First If
  2. If-Else
  3. Comparing
  4. Else-If
Exercise 1 of 4

Your First If

With if, code only runs when a condition is true:

if (age >= 18) {
    IO.println("Adult");
}

The code checks whether number is greater than 10. Change number so the text gets printed.

Starter code
void main() {
    int number = 5;

    if (number > 10) {
        IO.println("Greater than 10!");
    }
}
Background
How the compiler handles conditions

In bytecode, an if turns into a conditional jump. The CPU checks a comparison and either jumps into the "then" block or skips right over it.

Modern CPUs do branch prediction — they guess which branch is more likely and start preparing that code early. If the CPU guesses wrong, the work gets thrown away (pipeline flush).

Exercise 2 of 4

If-Else

With else you say what should happen when the condition is not true:

if (condition) {
    // if true
} else {
    // if false
}

The println lines are already there. Just fill in the condition of the if: check whether temperature is greater than or equal to 20.

Starter code
void main() {
    int temperature = 22;

    if (???) {
        IO.println("Warm");
    } else {
        IO.println("Cold");
    }
}
Background
The ternary operator — if-else in short

Java has a one-liner version of if-else: result = condition ? ifTrue : ifFalse;

Example: String s = temperature >= 20 ? "Warm" : "Cold"; — same decision as the if-else block here, just more compact.

Exercise 3 of 4

Comparing

Java has these comparison operators:

  • == equal to
  • != not equal to
  • < less than
  • > greater than
  • <= less than or equal to
  • >= greater than or equal to

Watch out: checking for equality is == (double!), not = (that's assignment).

Fill in the condition: check whether password has the value 1234.

Starter code
void main() {
    int password = 1234;

    if (???) {
        IO.println("Access granted");
    } else {
        IO.println("Wrong password");
    }
}
Background
== vs. .equals() — the most important Java trap

For primitive types (int, double, boolean), == compares the values — that's exactly right.

For objects (String, arrays, your own classes), == compares the reference (is it the very same object in memory?), not the content! For strings and most objects you need .equals(). Arrays are special: their .equals() also just compares references — use Arrays.equals() instead. This is one of the most common beginner mistakes in Java.

Exercise 4 of 4

Else-If

For multiple conditions, use else if:

if (grade == 1) {
    // ...
} else if (grade == 2) {
    // ...
} else {
    // everything else
}

Add the missing else if block: if grade equals 2, print Good.

Starter code
void main() {
    int grade = 2;

    if (grade == 1) {
        IO.println("Very good");
    // Add here: else if grade == 2 -> "Good"
    } else {
        IO.println("Not bad");
    }
}
Background
Else-if chains vs. switch

Long else if chains get messy fast. From about 4 cases on, a switch is often easier to read. Since Java 14 there are even switch expressions with arrow syntax: case 1 -> "Very good";

The compiler turns a switch into a jump table (the tableswitch / lookupswitch bytecode instructions) — so with many cases, a switch is usually faster than a long if-else chain, too.

Enough reading. Your turn.

In the tutorial you write the code yourself, hit Run and instantly see whether your program works. No JDK, no IDE, no bullshit.

Practice If-Else in your browser