Iteration Examples in CS Thinking

Start with the recap, study the fully worked examples, then use the practice problems to check your understanding of Iteration.

This page combines explanation, solved examples, and follow-up practice so you can move from recognition to confident problem-solving in CS Thinking.

Concept Recap

Repeating a block of instructions multiple times until a stopping condition is satisfied.

Do this again and again until some condition is met, then move on to the next step.

Read the full concept explanation β†’

How to Use These Examples

  • Read the first worked example with the solution open so the structure is clear.
  • Try the practice problems before revealing each solution.
  • Use the related concepts and background knowledge badges if you feel stuck.

What to Focus On

Core idea: Loops let you do repetitive tasks without writing repetitive code.

Common stuck point: The loop must have a way to stopβ€”every iteration should progress toward the stopping condition.

Worked Examples

Example 1

easy
Trace: FOR i = 1 TO 4: OUTPUT i * 2.

Solution

  1. 1
    Step 1: i = 1: output 2. i = 2: output 4.
  2. 2
    Step 2: i = 3: output 6. i = 4: output 8.
  3. 3
    Step 3: Loop ends after i = 4. Outputs: 2, 4, 6, 8.

Answer

2, 4, 6, 8
Iteration (looping) repeats a block of code a specified number of times. A FOR loop is used when we know in advance how many repetitions are needed.

Example 2

medium
Trace: SET count = 0. SET n = 1. WHILE n <= 10: IF n MOD 2 == 0 THEN count = count + 1. n = n + 1. OUTPUT count.

Practice Problems

Try these problems on your own first, then open the solution to compare your method.

Example 1

easy
Write pseudocode using a loop to output the numbers 5, 10, 15, 20, 25.

Example 2

easy
Trace this loop: SET total = 2. REPEAT 4 TIMES: SET total = total + 3. OUTPUT total.

Background Knowledge

These ideas may be useful before you work through the harder examples.

sequence