Control Statements in Java
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
| Family | Question it answers | Statements | Lesson |
|---|---|---|---|
| Decision | Which 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" |
| Jump | Should 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.
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");
}
}
}Seat 3 is free, no window
Window seat free: 5How it works
- Line 6 is a loop: it repeats the block for seats 0 to 4.
- 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. - Line 10 is another decision: if it's a window seat, print it, and line 12's
break(a jump) ends the loop early. - 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:
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
- 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".
- Harder: Change
FindSeatso it prints every free window seat instead of stopping at the first one. Which statement do you remove?
Key Takeaways
Was this tutorial helpful?