Conditionals

A program that always does exactly the same thing regardless of input isn't very useful. Conditionals are how you tell Java "do this, but only under these circumstances" — and how "circumstances" branch out into a chain of possibilities.

if, else if, else

An if statement runs a block only when its condition is true. Chain else if onto it to check additional conditions in order, and finish with a plain else to catch everything that didn't match:

</> GradeClassifier.java
public class GradeClassifier {
    public static void main(String[] args) {
        int score = 78;
        String letter;

        if (score >= 90) {
            letter = "A";
        } else if (score >= 80) {
            letter = "B";
        } else if (score >= 70) {
            letter = "C";
        } else if (score >= 60) {
            letter = "D";
        } else {
            letter = "F";
        }

        System.out.println("Score " + score + " maps to grade " + letter);
    }
}
Output
Score 78 maps to grade C

Order matters here in a way that's easy to overlook. Java checks each condition top to bottom and stops at the first one that's true, so score >= 70 only ever gets evaluated once we already know score is less than 80 — that's why it's safe to write it without also saying score < 80. Flip the order and put the loosest condition first, and every score would incorrectly match it.

Braces aren't optional in practice

Java technically allows you to drop the curly braces when a branch is a single statement. Avoid doing this. It reads fine until someone — often you, months later — adds a second line inside the if and forgets that only the first line was ever actually conditional:

</> BraceTrap.java
// Looks like both lines are conditional. They are not.
if (score > 100)
    System.out.println("Invalid score");
    System.out.println("Discarding entry");

The second println runs every single time, regardless of score, because indentation has no effect on how Java groups statements — only braces do. Wrapping both lines in { } would have prevented this entirely.

The switch statement

When you're comparing one value against a long list of exact possibilities, a chain of else if gets repetitive fast. switch handles that shape more directly:

</> ShippingCost.java
public class ShippingCost {
    public static void main(String[] args) {
        String method = "express";
        double cost;

        switch (method) {
            case "standard":
                cost = 4.99;
                break;
            case "express":
                cost = 12.99;
                break;
            case "overnight":
                cost = 24.99;
                break;
            default:
                cost = 0.0;
        }

        System.out.println("Shipping cost: $" + cost);
    }
}
Output
Shipping cost: $12.99

Each case checks method against one exact value. The break at the end of each case is what stops execution from falling through into the next one — leave it out, and Java keeps running the following cases' code regardless of whether they matched, all the way down until it hits a break or reaches the end. That fall-through behavior is occasionally useful on purpose, but far more often it's a bug waiting to happen, which is why modern Java added a safer alternative.

The modern switch expression

Since Java 14, switch can also be written as an expression that produces a value directly, using -> instead of case ... :. There's no fall-through to worry about, and no break needed:

</> ShippingCostModern.java
String method = "overnight";

double cost = switch (method) {
    case "standard" -> 4.99;
    case "express" -> 12.99;
    case "overnight" -> 24.99;
    default -> 0.0;
};

System.out.println("Shipping cost: $" + cost);
Output
Shipping cost: $24.99
Note: a switch on a String or int works fine, but it always needs a value that can be checked for exact equality — you can't use a switch to test ranges like "score between 70 and 79" the way the if/else if chain above did. Pick whichever form matches the shape of the decision you're actually making.