Programming Companion
Java

Programming technique

Counted loops

A counted loop repeats a block a known number of times. In Java, a for loop usually combines the counter's starting value, continuation condition and update.

A counted loop repeats a block a known number of times. In Python, a for loop commonly takes each value produced by range().

IB DP CS standard B2.3.3: Construct programs that use looping structures to perform repeated actions.

The three parts of a for loop

Choose the values with range

  1. int number = 1 runs once before the loop begins.
  2. number <= 5 is checked before each repetition.
  3. number++ runs after each completed repetition.
  1. range(1, 6) starts at 1.
  2. The stop value 6 is excluded, so the generated values are 1 through 5.
  3. number receives the next generated value before each repetition.
Trace the counter before guessing the output
Checknumbernumber <= 5Output
11true1
22true2
33true3
44true4
55true5
66falseLoop stops

The value 6 is still checked, but it never enters the loop body.

Trace the values generated by range
RepetitionnumberOutput
111
222
333
444
555
StopNo next valueLoop stops

range(1, 6) never produces 6 because the stop value is excluded.

Accumulators

An accumulator stores a running total. Initialise it before the loop and update it inside the loop.

Trace the boundary values

Check the first counter value, the last value that enters the loop and the first value that fails the condition. This catches many off-by-one errors.

Check the first value, the last generated value and the excluded stop value. This catches many off-by-one errors.

Known repetitions

Use a counted loop when the number of repetitions is known before the loop starts.

Update the correct variable

Changing the wrong variable can create an infinite loop or incorrect output.

Choose range carefully

Remember that the stop value is excluded and an optional third argument controls the step.

Keep loop work focused

Only place statements inside the loop when they genuinely need to repeat.

Challenges Choose one

Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.

Square Numbers

Challenge ID: PC-T07-C01 · Standards: B2.3.3

Output every whole number from 1 to 20 together with its square. Use one counted loop and make the output easy to read.

Times Table

Challenge ID: PC-T07-C02 · Standards: B2.3.3

Ask the user for a number from 1 to 12. Output that number's multiplication table from 1 to 12 using a counted loop.

FizzBuzz

Challenge ID: PC-T07-C03 · Standards: B2.3.2, B2.3.3

Output the numbers from 1 to 100. Output Fizz for multiples of 3, Buzz for multiples of 5 and FizzBuzz for multiples of both. Ensure the combined case is tested before the individual cases.

Numbered tiles in a FizzBuzz sequence, including Fizz, Buzz and FizzBuzz.

ROT13

Challenge ID: PC-T07-C04 · Standards: B2.1.2, B2.3.2, B2.3.3

Ask the user for a line of text and apply the ROT13 substitution to every letter. Preserve spaces, punctuation and letter case. The same program should decode ROT13 text because applying ROT13 twice restores the original text.