Iteration Examples in CS Thinking: 47 Problems with Answers

Worked examples of Iteration to try before you open each solution, then practice problems with answers.

Worked Examples

Example 1

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

Answer

2, 4, 6, 8

First step

1.
Step 1: i = 1: output 2. i = 2: output 4.

Full solution

  1. 2.
    Step 2: i = 3: output 6. i = 4: output 8.
  2. 3.
    Step 3: Loop ends after i = 4. Outputs: 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.

Example 3

medium
Trace nested: for i in 1..3: for j in 1..2: count += 1. Final count?

Example 4

medium
Trace: count = 0; for i in 1..50: if i mod 7==0: count += 1. Final count?

Example 5

medium
Trace: i=1; s=0; while i≤5: s=s+i⋅i; i=i+1. Final s?

Example 6

medium
Trace: max = -infinity; for x in [3,7,2,9,4]: if x> max: max = x. Final max?

Example 7

medium
Trace: s=0; for i in 1..10: if i mod 2==1: s=s+i. Final s?

Example 8

hard
Trace Fibonacci: a=0,b=1; repeat 5 times: t=a+b; a=b; b=t. Final b?

Example 9

hard
Trace: list = [5,3,8,1,9,2]; find min via single pass. What is min?

Example 10

hard
Trace: list = [4,2,7,1,8]. Compute average via iteration: sum then divide by count.

Example 11

hard
Trace string concatenation: s= '' ; for c in 'abc': s=s+c. Final s?

Example 12

challenge
Trace: n=27; count = 0; while n>1: if n even: n=n//2; else: n=3⋅n+1; count += 1. What is count when the loop ends? (Collatz)

Practice Problems

Try each one, 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.

Example 3

easy
Trace: s=0; for i in 1,2,3: s=s+i. Final s?

Example 4

easy
How many times does 'for i in range(5)' run the loop body? (range(5) is 0..4)

Example 5

easy
Trace: i=0; while i<3: i=i+1. Final i?

Example 6

easy
Trace: count =0; for x in [4,7,1,9]: count = count + 1. Final count?

Example 7

easy
Which keyword typically starts a loop that repeats while a condition is true?

Example 8

easy
Trace: p=1; for i in 1,2,3: p=p∗2. Final p?

Example 9

easy
A loop never updates its condition variable. What problem occurs?

Example 10

easy
Trace: for i in range(3): print i (range(3) is 0,1,2). What is printed in order?

Example 11

medium
Trace: s=0; for i in range(1,5): s=s+i (range(1,5) is 1,2,3,4). Final s?

Example 12

medium
Nested loops: for i in range(3): for j in range(2): count+=1. Final count?

Example 13

medium
Trace: i=10; while i>0: i=i−3; print final i (when loop ends).

Example 14

medium
How many times does 'for i in range(2, 10, 2)' run? (start 2, stop 10, step 2)

Example 15

medium
A loop sums even numbers in [1,2,3,4,5,6]. Trace and give the total.

Example 16

medium
Trace: a list [1,2,3]; for each element, append element*element to a new list. What is the new list?

Example 17

medium
A while-loop reads numbers until it reads 0 (a sentinel). For inputs 5, 3, 0, how many numbers are processed before stopping?

Example 18

medium
Trace: s=0; for i in range(4): s=s+i (range(4) is 0,1,2,3). Final s?

Example 19

medium
Trace: i=1; f=1; while i≤3: f=f∗i; i=i+1. Final f?

Example 20

challenge
Trace: a=0,b=1; repeat 4 times: t=a+b; a=b; b=t. Final b?

Example 21

challenge
A list [1,2,3,4]; a loop removes every element while iterating index 0..len. Why does this skip elements?

Example 22

challenge
Two loops sum 1..n: loop A runs n times, loop B is nested running n*n times. For large n, which dominates the running time and what is B's growth class?

Example 23

easy
Trace: s=0; for i in 1,2,3,4: s=s+i. Final s?

Example 24

easy
How many times does 'for i in range(6)' run the body?

Example 25

easy
Trace: c=0; for x in [2,4,6,8,10]: c=c+1. Final c?

Example 26

easy
Trace: i=5; while i>0: i=i−1. Final i?

Example 27

easy
Trace: p=1; for i in 1,2,3,4: p=p∗i. Final p?

Example 28

medium
Trace: s=0; for i in range(1, 11): s=s+i. Final s?

Example 29

medium
How many times does 'for i in range(3, 11, 2)' run?

Example 30

medium
A loop with a 'break' statement that fires after 5 iterations of a planned 100 — how many times does the body execute?

Example 31

medium
What is the difference between break and continue?

Example 32

hard
Why does modifying a list while iterating over it often cause bugs?

Example 33

hard
A nested loop runs an outer loop n times and an inner loop n times. What is the running time class?

Example 34

hard
A while loop with condition i<10 but i never changes inside. What problem and what fix?

Example 35

challenge
A program needs to compute n! for n=10 iteratively. Why is iteration preferred over recursion here in many languages?

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.

Ask yourself: Does a block of steps run again and again until a count or condition says stop?

Concept recap

Repeating a block of instructions multiple times until a stopping condition is satisfied. Iteration is one of the three fundamental control structures (along with sequence and selection) and is implemented through while loops, for loops, and other looping constructs.

A loop is like running laps with a coach holding a clipboard. Before each lap the coach checks the rule, say 'fewer than 4 laps done?', and you run again only if the answer is yes. Each lap adds a tick to the clipboard. Forget the tick and the coach will keep you running forever.

Read the full lesson on Iteration

Common mistakes to watch for

Background knowledge

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

sequence