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
int number = 1 runs once before the loop begins.
number <= 5 is checked before each repetition.
number++ runs after each completed repetition.
range(1, 6) starts at 1.
The stop value 6 is excluded, so the generated values are 1 through 5.
number receives the next generated value before each repetition.
Trace the counter before guessing the output
Check
number
number <= 5
Output
1
1
true
1
2
2
true
2
3
3
true
3
4
4
true
4
5
5
true
5
6
6
false
Loop stops
The value 6 is still checked, but it never enters the loop body.
Trace the values generated by range
Repetition
number
Output
1
1
1
2
2
2
3
3
3
4
4
4
5
5
5
Stop
No next value
Loop 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.
ChallengesChoose one
Choose a challenge that feels appropriate for you. Code heat is only a rough estimate, not a fixed level.
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.
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.
construction
Scaffold available
encrypted
Selected challenge
ID:Language:Code heat:Standards:
1
Write your first version by hand
Before opening your IDE, write your first attempt at this program on paper using Java. It does not need to be perfect.
Required: Keep the paper beside you while you code because you will compare it with your final program before submitting. At the end of the lesson, hand the paper to your teacher in person. You do not upload the paper through this site.
At the top of your paper write:Your nameTASK_CODE
Not marked complete. Mark it complete when your handwritten code is ready.
2
Open the challenge
Code Companion Local creates the starter file in the exact managed challenge folder and opens it in your preferred IDE. You do not need to create folders, files or run terminal commands.
Mark your handwritten code complete first.
If you are simply browsing the public course and are not using a class, complete the challenge in any editor you like. Code Companion Local is only needed for the connected classroom workflow.
3
Complete the challenge
Use your handwritten code as the starting point, then write and test your solution in the challenge workspace. Code Companion Local will ask you to compare the two versions before you submit.
construction
Optional two-level scaffold
Try from your handwritten pseudocode first. Scaffold gives some structure; Scaffold + comments gives stronger guidance. Use only the level you need, and update your pseudocode first if the support changes your plan.