For Loop Examples: 48 Problems with Answers

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

Worked Examples

Example 1

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

Answer

0, 1, 4, 9, 16

First step

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

Full solution

  1. 2.
    Step 2: i=3: output 9. i=4: output 16.
  2. 3.
    Step 3: Outputs: 0, 1, 4, 9, 16 (perfect squares).
FOR loops are used when the number of iterations is known in advance. The loop variable automatically increments each iteration.

Example 2

medium
Convert this FOR loop to a WHILE loop: FOR i = 1 TO 5: OUTPUT i.

Example 3

medium
Trace: `total=0; for i in range(2,8): total+=i`. Final total?

Example 4

medium
Trace nested: `c=0; for i in range(4): for j in range(3): c+=1`. Final c?

Example 5

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

Example 6

medium
Trace: `nums=[3,1,4,1,5]; mx=nums[0]; for x in nums[1:]: if x>mx: mx=x`. Final mx?

Example 7

medium
Trace: `p=1; for i in range(1,6): if i%2==0: p*=i`. Final p?

Example 8

medium
Trace: `out=[]; for x in [1,2,3]: for y in [10,20]: out.append(x+y)`. Final out?

Example 9

medium
Trace: `out=[]; for i in range(4): out.append(i*(i+1))`. Final out?

Example 10

hard
Trace: `out=[]; for i in range(5): for j in range(5): if i+j==4: out.append((i,j))`. Final out?

Example 11

hard
Trace: `s=0; for i in range(1,6): s += i*i`. Final s?

Example 12

hard
Trace: `n=5; out=[]; for i in range(n): row=[]; for j in range(n): row.append(1 if i==j else 0); out.append(row)`. How many 1s in out?

Example 13

challenge
Trace: `out=[]; for i in range(1,5): for j in range(1,5): if i!=j and (i+j)%2==0: out.append((i,j))`. How many tuples?

Practice Problems

Try each one, then open the solution to compare your method.

Example 1

medium
Write a FOR loop that calculates 5!=5×4×3×2×1.

Example 2

medium
What does this code output?

SET scores = [85, 92, 78, 95, 88]
SET highest = scores[0]
FOR i = 1 TO LENGTH(scores) - 1
IF scores[i] > highest THEN
SET highest = scores[i]
END IF
END FOR
OUTPUT highest

Example 3

easy
What values does `for i in range(5)` give i?

Example 4

easy
How many times does `for i in range(5)` iterate?

Example 5

easy
Trace: `total=0; for i in range(4): total+=i`. Final total?

Example 6

easy
What does `range(2, 6)` produce?

Example 7

easy
What does `range(0, 10, 2)` produce?

Example 8

easy
Trace: `out=[]; for c in 'cat': out.append(c)`. Final out?

Example 9

easy
Trace: `p=1; for i in range(1,4): p*=i`. Final p?

Example 10

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

Example 11

medium
Trace: `total=0; for i in range(1,6): total+=i`. Final total?

Example 12

medium
Trace: nested `c=0; for i in range(3): for j in range(2): c+=1`. Final c?

Example 13

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

Example 14

medium
Trace: `out=[]; for i in range(3): for j in range(3): if i==j: out.append((i,j))`. Final out?

Example 15

medium
Trace: `out=[]; for i in range(4): out.append(i*i)`. Final out?

Example 16

medium
Trace: `for i in range(5,0,-1): print(i)`. What prints?

Example 17

medium
Trace: `out=[]; nums=[1,2,3]; for i in range(len(nums)): out.append(nums[i]*2)`. Final out?

Example 18

medium
Trace: `seen=[]; for x in [1,2,2,3,3,3]: if x not in seen: seen.append(x)`. Final seen?

Example 19

medium
Trace: `total=0; for i in range(10): if i==5: continue; total+=i`. Final total?

Example 20

challenge
Trace: `out=[]; data=[1,2,3,4]; for x in data: if x%2==0: data.remove(x)`. What is data afterward, and why is this buggy?

Example 21

challenge
Trace: `m=[[1,2],[3,4],[5,6]]; total=0; for row in m: for v in row: total+=v`. Final total, and how many additions occur?

Example 22

challenge
Trace: `out=[]; for i in range(1,4): for j in range(i+1,4): out.append((i,j))`. Final out and its length?

Example 23

easy
How many iterations does `for i in range(7)` perform?

Example 24

easy
Trace: `total=0; for i in range(5): total+=2`. Final total?

Example 25

easy
Trace: `s=''; for c in 'abc': s = c + s`. Final s?

Example 26

easy
Trace: `out=[]; for x in [3,1,4]: out.append(x+1)`. Final out?

Example 27

easy
Trace: `count=0; for x in [2,4,6,8]: if x%2==0: count+=1`. Final count?

Example 28

easy
Why might `for i in range(1, 5)` instead of `for i in range(5)` be the better choice?

Example 29

medium
Trace: `out=[]; for i in range(5): if i%2: out.append(i)`. Final out?

Example 30

medium
Trace: `for i in range(3, 12, 3): print(i)`. What prints?

Example 31

medium
Trace: `total=0; for i in range(1,11): if i==7: break; total+=i`. Final total?

Example 32

medium
When is a `while` loop preferable to a `for` loop?

Example 33

hard
What's wrong with: `nums = [1,2,3,4]; for x in nums: nums.append(x)`?

Example 34

hard
What is the Big-O time of two nested for loops, each running n times, with O(1) work inside?

Example 35

challenge
A for loop over a list of n items contains another for loop over the same list. Each iteration does O(1) work. What is the total time complexity in terms of n?

What to focus on

Core idea: For loops are best when you know how many times to repeat or when iterating over a collection. The loop variable takes each value automatically.

Common stuck point: range(5) gives 0 to 4, not 1 to 5. Off-by-one errors are the most common for-loop bug.

Ask yourself: Do you know before the loop starts exactly what it goes through: a fixed count, or every item of a list or string?

Concept recap

A control structure that repeats a block of code a specific number of times or once for each item in a collection. The loop variable is automatically set to each successive value in the range or collection, eliminating the need to manually update it.

Dealing cards around a table is a for loop: one card to each player, and the round ends when every player has one, with nobody counting aloud. The players are the collection and dealing a card is the body. The picture also predicts the classic bug: if a player leaves mid-round, as when you remove items while looping, someone gets skipped.

Read the full lesson on For Loop

Related concepts

Background knowledge

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

iterationwhile loop