What you'll learn

  • if / else if / else and the ternary operator
  • Modern switch expressions with arrows
  • for, for-each and while loops
  • break and continue

Programs become useful when they make decisions (“is this a bulk order?”) and repeat work (“for every line on the order…”). That’s control flow.

Making decisions with if

int quantity = 120;
if (quantity > 100) {
    System.out.println("Bulk order");
} else if (quantity > 10) {
    System.out.println("Standard order");
} else {
    System.out.println("Small order");
}

Conditions use comparison operators — >, <, >=, <=, == (equal), != (not equal) — combined with && (and), || (or) and ! (not):

if (quantity > 0 && !cancelled) { ... }

Watch out: = assigns, == compares. And for Strings, use equals() — see the previous lesson.

The ternary operator

A compact if/else that returns a value:

String shipping = quantity > 50 ? "Free" : "Paid";

Switch expressions (Java 14+)

When you compare one value against many options, a switch expression is cleaner than a long if chain:

String message = switch (status) {
    case "NEW" -> "Order received";
    case "PICKING", "PACKING" -> "Being prepared";   // several values, one result
    case "SHIPPED" -> "On its way";
    default -> "Unknown status";
};

The arrow form has no “fall-through” surprises, and because it returns a value, the compiler makes sure every case produces one. (Older code uses case "NEW": ... break; — it works the same way but is easier to get wrong.)

Loops

for — when you know how many times

for (int i = 1; i <= 3; i++) {
    System.out.println("Line " + i);
}

Read it as: start at 1; keep going while i <= 3; add 1 after each round.

for-each — once per item in a collection

for (String item : List.of("AX-200", "BX-310")) {
    System.out.println(item);
}

This is the loop you’ll use most. Later you’ll meet streams, which often replace loops entirely.

while — repeat while a condition holds

int retries = 0;
while (retries < 3) {
    retries++;
    System.out.println("Attempt " + retries);
}

Make sure something inside the loop eventually makes the condition false, or it will run forever.

break and continue

for (int i = 1; i <= 10; i++) {
    if (i % 2 == 0) continue;   // skip to the next round
    if (i > 7) break;           // leave the loop
    System.out.println(i);      // 1 3 5 7
}

The full example

// ControlFlow.java — if/else, switch expressions and loops.
import java.util.List;

public class ControlFlow {

    public static void main(String[] args) {
        int quantity = 120;

        // if / else if / else
        if (quantity > 100) {
            System.out.println("Bulk order");
        } else if (quantity > 10) {
            System.out.println("Standard order");
        } else {
            System.out.println("Small order");
        }

        // Ternary operator: a compact if/else that returns a value
        String shipping = quantity > 50 ? "Free" : "Paid";
        System.out.println("Shipping: " + shipping);

        // Switch expression (Java 14+): no fall-through, returns a value
        String status = "SHIPPED";
        String message = switch (status) {
            case "NEW" -> "Order received";
            case "PICKING", "PACKING" -> "Being prepared";
            case "SHIPPED" -> "On its way";
            default -> "Unknown status";
        };
        System.out.println(message);

        // Classic for loop
        for (int i = 1; i <= 3; i++) {
            System.out.println("Line " + i);
        }

        // Enhanced for loop over a collection
        List<String> items = List.of("AX-200", "BX-310", "CX-420");
        for (String item : items) {
            System.out.println("Item: " + item);
        }

        // while loop
        int retries = 0;
        while (retries < 3) {
            retries++;
            System.out.println("Attempt " + retries);
        }

        // break and continue
        for (int i = 1; i <= 10; i++) {
            if (i % 2 == 0) continue;   // skip even numbers
            if (i > 7) break;           // stop the loop
            System.out.println("Odd: " + i);
        }
    }
}

Try it yourself

Print the numbers 1 to 15, but print Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both — the classic interview warm-up.

Show a solution
for (int i = 1; i <= 15; i++) {
    String out = (i % 15 == 0) ? "FizzBuzz"
               : (i % 3 == 0)  ? "Fizz"
               : (i % 5 == 0)  ? "Buzz"
               : String.valueOf(i);
    System.out.println(out);
}

Check multiples of 15 first — otherwise 15 would print just “Fizz”.

Key takeaways

  • if / else if / else for decisions; the ternary ? : for small either/or values.
  • Prefer the arrow switch expression for many-way choices.
  • for when you count, for-each for collections, while for “until something happens”.
  • break leaves a loop, continue skips to the next round.

Example files