Control Flow
How programs make decisions and repeat themselves — if/else, switch, while, do-while, for, and the loops that quietly run the world
A program made only of straight-line statements is uninteresting. The moment a program needs to decide or repeat, you reach for control flow: the constructs that change which line runs next.
if and else
if runs a block of code only when a condition is true. else runs
when the condition is false. else if chains additional conditions.
Conditions are expressions of type boolean. The comparison
operators that produce booleans are ==, !=, <, <=, >,
>=. Combine booleans with && (and), || (or), ! (not).
Remember:
==on objects compares references, not contents. For strings, use.equals(...). For primitives,==does the obvious thing.
switch
When you want to branch on a value, switch is often clearer than
a long if/else chain.
A few things to remember about switch:
- Without
break, execution falls through to the next case. That is occasionally useful and frequently a bug. - Modern Java has a much nicer
switchexpression with->arrows and no fall-through. We use the classic form here because it is the one you will see most in existing code.
while and do-while
while repeats a block as long as a condition is true. The
condition is checked before each pass.
do-while is the same, except the condition is checked after
each pass — so the block always runs at least once.
for
for is the workhorse of loops. It packages three things on one line:
- Initialization — runs once at the start.
- Condition — checked before each pass.
- Update — runs at the end of each pass.
i++ is shorthand for i = i + 1. Likewise i--, i += 2, i *= 3, etc.
The enhanced for (for-each)
For going through every element of an array or collection, Java has a cleaner form:
Read for (int p : primes) as "for each int p in primes."
break and continue
breakexits the innermost loop immediately.continuejumps to the next iteration of the innermost loop.
Use these sparingly. Loops are easier to reason about when the condition itself expresses why they end.
A tracing exercise
Try to predict the output of this program before you run it. Then run it and check.
This is a nested loop — a loop inside a loop. Each value of the
outer i triggers a full inner sweep over j. Read it slowly. The
ability to mentally execute a nested loop is one of the major
beginner milestones.
What's the difference between while and do-while?
They are the same
while is faster than do-while
while checks the condition before the first iteration; do-while checks after, so its body always runs at least once
do-while cannot have a condition
What does for (int x : arr) do?
Loops the index from x to arr.length
Creates a new array x with the same contents as arr
Iterates over each element of arr in order, binding the current element to x
Sorts the array
A small challenge: FizzBuzz
A classic. Print the numbers 1 through 15, but: replace multiples of
3 with Fizz, multiples of 5 with Buzz, and multiples of both with
FizzBuzz.
Print the numbers from 1 to 15, one per line. But:
- If a number is divisible by both 3 and 5, print
FizzBuzzinstead. - If divisible only by 3, print
Fizz. - If divisible only by 5, print
Buzz. - Otherwise, print the number itself.
The first lines of correct output are:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
Loops and conditions are the machinery. Methods are the way we package that machinery into reusable, named units. That is next.
The Type System
Why every value in Java has a type, what the compiler checks for you, and how types make large programs trustworthy
Methods and Modularity
Why we group instructions into named methods, how they call each other, and how a small program can be a graceful conversation between many small methods