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. 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.

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.

Sense of Study hint: When writing a loop, first decide what action needs repeating and what condition should stop the repetition. Initialize any loop variables before the loop starts, and make sure the loop body changes something so the stopping condition is eventually reached.

Common Mistakes to Watch For

Before you work through the examples, skim the mistake guide so you know which shortcuts and sign errors to avoid.

Worked Examples

Example 1

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

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 ii in 1..3: for jj in 1..2: count += 1. Final count?

Example 4

medium
Trace: count = 0; for ii in 1..50: if iβ€Šmodβ€Š7==0i \bmod 7 == 0: count += 1. Final count?

Example 5

medium
Trace: i=1i = 1; s=0s = 0; while i≀5i \le 5: s=s+iβ‹…is = s + i \cdot i; i=i+1i = i + 1. Final ss?

Example 6

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

Example 7

medium
Trace: s=0s = 0; for ii in 1..10: if iβ€Šmodβ€Š2==1i \bmod 2 == 1: s=s+is = s + i. Final ss?

Example 8

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

Example 9

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

Example 10

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

Example 11

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

Example 12

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

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.

Example 3

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

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=0i=0; while i<3i < 3: i=i+1i = i + 1. Final ii?

Example 6

easy
Trace: count =0=0; for xx in [4,7,1,9][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=1p=1; for ii in 1,2,31,2,3: p=pβˆ—2p = p * 2. Final pp?

Example 9

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

Example 10

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

Example 11

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

Example 12

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

Example 13

medium
Trace: i=10i=10; while i>0i > 0: i=iβˆ’3i = i - 3; print final ii (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][1,2,3,4,5,6]. Trace and give the total.

Example 16

medium
Trace: a list [1,2,3][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=0s=0; for ii in range(4): s=s+is = s + i (range(4) is 0,1,2,3). Final ss?

Example 19

medium
Trace: i=1i=1; f=1f=1; while i≀3i \le 3: f=fβˆ—if = f * i; i=i+1i = i + 1. Final ff?

Example 20

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

Example 21

challenge
A list [1,2,3,4][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=0s=0; for ii in 1,2,3,41,2,3,4: s=s+is = s + i. Final ss?

Example 24

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

Example 25

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

Example 26

easy
Trace: i=5i = 5; while i>0i > 0: i=iβˆ’1i = i - 1. Final ii?

Example 27

easy
Trace: p=1p = 1; for ii in 1,2,3,41,2,3,4: p=pβˆ—ip = p * i. Final pp?

Example 28

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

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 nn times and an inner loop nn times. What is the running time class?

Example 34

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

Example 35

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

Background Knowledge

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

sequence