Dataslope logoDataslope

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.

Code Block
Java 8 (Update 492)

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.

Code Block
Java 8 (Update 492)

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 switch expression 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.

Code Block
Java 8 (Update 492)

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:

  1. Initialization — runs once at the start.
  2. Condition — checked before each pass.
  3. Update — runs at the end of each pass.
Code Block
Java 8 (Update 492)

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:

Code Block
Java 8 (Update 492)

Read for (int p : primes) as "for each int p in primes."

break and continue

  • break exits the innermost loop immediately.
  • continue jumps to the next iteration of the innermost loop.
Code Block
Java 8 (Update 492)

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.

Code Block
Java 8 (Update 492)

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.

QuestionSelect one

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

QuestionSelect one

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.

Challenge
Java 8 (Update 492)
FizzBuzz

Print the numbers from 1 to 15, one per line. But:

  • If a number is divisible by both 3 and 5, print FizzBuzz instead.
  • 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.

On this page