Control Statements in Java

Naveen AnamaneniUpdated · 4 min read

Every program you've written so far runs from the first line of main to the last, in order, exactly once. Real programs can't work like that. A ticket app must decide whether a seat is free, repeat the same check for every seat in a coach, and stop searching once it finds one. The statements that change the order in which lines run are called control statements, and this chapter covers all of them.

Three Families

Decide, repeat, jump
FamilyQuestion it answersStatementsLesson
DecisionWhich path should run?if, if-else, else if, switch"if, if-else and else-if", "Switch Statement", "Switch Expressions"
Loop (iteration)How many times should this run?for, for-each, while, do-while"for Loop" to "do-while Loop"
JumpShould we leave or skip early?break, continue, return"break Statement" to "return Statement"

All Three in One Program

This program checks a coach's seats one by one, skips the booked ones, and stops at the first free window seat.

FindSeat.java▶ Try it
public class FindSeat {
    public static void main(String[] args) {
        boolean[] booked = {true, true, false, true, false};
        boolean[] window = {true, false, false, true, true};
 
        for (int seat = 0; seat < booked.length; seat++) {
            if (booked[seat]) {
                continue;
            }
            if (window[seat]) {
                System.out.println("Window seat free: " + (seat + 1));
                break;
            }
            System.out.println("Seat " + (seat + 1) + " is free, no window");
        }
    }
}
Output
Seat 3 is free, no window
Window seat free: 5

How it works

  1. Line 6 is a loop: it repeats the block for seats 0 to 4.
  2. Line 7 is a decision: if the seat is booked, line 8's continue (a jump) skips the rest and moves to the next seat.
  3. Line 10 is another decision: if it's a window seat, print it, and line 12's break (a jump) ends the loop early.
  4. Seat 3 (index 2) is free but not a window, so it's reported and the loop carries on. Seat 5 (index 4) is free and a window, so the loop stops.

The arrays on lines 3 and 4 hold several values each; The lesson "Java Arrays" covers them. Seat numbers print as seat + 1 because array positions start at 0.

Try it yourself: change window[4] to false. What does the program print now, and why is there no "Window seat free" line?

Conditions Are boolean

Every decision and almost every loop depends on a condition: an expression that's true or false. Java is strict about this. if (seats) with an int doesn't compile, unlike in C, where any non-zero number counts as true:

Compiler output
IfAssign.java:4: error: incompatible types: int cannot be converted to boolean
        if (x = 5) {
              ^

That strictness also catches the classic typo of = (assign) instead of == (compare).

Common Mistakes

Using = instead of == in a condition. Java rejects it for numbers, as shown above. With boolean variables, if (done = true) compiles and always runs, so write if (done) instead.

Putting a semicolon after the condition. if (booked); { ... } ends the if at the semicolon, and the block always runs. The same happens with for (...); and while (...);.

Choosing a loop when a decision is enough, or the other way round. "Do this if..." is a decision; "do this for each..." or "until..." is a loop.

Practice

  1. Easy: For each sentence, name the family: "If the payment fails, show an error"; "Send a reminder to every passenger"; "Stop searching once you find the ticket".
  2. Harder: Change FindSeat so it prints every free window seat instead of stopping at the first one. Which statement do you remove?

Quick check: Control statements

4 questions
1Which family does while belong to?
2What does continue do inside a loop?
3Why doesn't if (5) compile in Java?
4Which statement ends a loop immediately?

Key Takeaways

Was this tutorial helpful?

Lesson 53 of 482