Skip to lesson

Common mistakes and practice

A review of this module. Skip it if you feel confident; the next module does not need it.

Fix the mistakes

The slips that cost marks in this module. Spot each one, then check the fix.

Most loop bugs are one of a few kinds: one round too many or too few, a loop that never stops, or a counter changed in the wrong place. Spot them quickly, then put the whole module to work on real problems.

Mistakes and fixes at a glance

Most loop bugs come from where a loop starts, when it stops, or how it moves on. The most common is off-by-one: the loop runs one round too many or too few.

Example · Off-by-one: < vs <=

OffByOne.javaJava
public class OffByOne {    public static void main(String[] args) {        for (int i = 1; i < 5; i++) {            System.out.print(i + " ");        }        System.out.println();        for (int i = 1; i <= 5; i++) {            System.out.print(i + " ");        }        System.out.println();    }}
Output
1 2 3 4
1 2 3 4 5

What this code does

  • With <, the round for 5 never happens, because 5 < 5 is false.
  • To check any loop, test its last round: put the last value into the condition and see if it is true.

Review: The for loop (opens in a new tab). The other common mistakes, each with what goes wrong and the fix:

  • No update, or an update in the wrong direction → never stops → move the counter towards the end. Review: while and do-while (opens in a new tab)
  • ; right after for (…) → the loop repeats nothing, and the block below runs once → remove the ;.
  • An extra i++ inside a for body → every other number is skipped → update in one place only.
  • Total declared inside the loop → resets every round → declare it before. Review: Tracing a loop (opens in a new tab)
  • continue before the update in a while loop, or break outside its if → never stops, or stops at once. Review: break and continue (opens in a new tab)

Spot the bug

Each program below looks almost right but does the wrong thing. Read what it should do, tap the line that is wrong, then check the fix.

Before you tap, ask the three loop questions: where does it start, when does it stop, and does every round move towards the stop?

Find the bug

Find the loop bug

Select the line that is wrong.

This should print the numbers 1 to 10.

Round 1 of 6

When in doubt, trace it

A loop gives a wrong answer and you cannot see why. Reading the code again rarely helps, because you read what you meant, not what Java does.

Trace it instead: follow the loop step by step and write down every variable. Try it on a faster GCD than the subtraction one in while and do-while (opens in a new tab): each round replaces the pair (a, b) with (b, a % b) until b is 0.

Trace the code

Trace a GCD loop with %

public class FastGcd {
public static void main(String[] args) {
int a = 48;
int b = 18;
while (b != 0) {
int r = a % b;
a = b;
b = r;
}
System.out.println("GCD: " + a);
}
}

Variables

a
48

Output

(nothing printed yet)

Line 3a starts at 48.

Step 1 of 16

Finish up

Mark complete & continue

Next: Must-do problems: Loops