While Loop Examples: 49 Problems with Answers

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

Worked Examples

Example 1

medium
Trace: SET n = 1. WHILE n < 100: n = n * 2. OUTPUT n. How many iterations?
Show solution

Answer

Output: 128. 7 iterations.

First step

1.
Step 1: Start at n=1 and double each time: 1→2→4→8→16→32→64→128.

See the full worked solution + why-it-works coaching

SetupKey insightWhy it worksCommon pitfallConnection

Free: full solutions on 3 topics a day. No card.

Every topic, every day: the Family plan

Example 2

medium
Write a WHILE loop that asks the user for input until they type 'quit'.

Practice Problems

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

Example 1

medium
Trace: SET total = 0. SET i = 1. WHILE i <= 4: total = total + i. i = i + 1. OUTPUT total.

Example 2

medium
Trace this code and state the final output:

SET n = 1
SET result = 1
WHILE n < 5
SET n = n + 1
SET result = result * n
END WHILE
OUTPUT result

Example 3

easy
Trace: `i=0; while i<3: print(i); i+=1`. What values print?

Example 4

easy
How many times does the body run? `i=1; while i<=5: i+=1`.

Example 5

easy
Trace: `n=10; while n>0: n-=3; print(n)`. What prints?

Example 6

easy
Does this loop body ever run? `x=5; while x<3: print('hi')`.

Example 7

easy
Trace: `i=0; total=0; while i<4: total+=i; i+=1`. Final total?

Example 8

easy
Spot the bug: `i=0; while i<3: print(i)`. What goes wrong?

Example 9

easy
Trace: `p=1; while p<20: p*=2; print(p)`. What prints?

Example 10

easy
Trace: `s='abc'; i=0; out=''; while i<len(s): out=s[i]+out; i+=1`. Final out?

Example 11

medium
Trace: `i=1; while i<=8: i*=2; print(i)`. What prints and how many iterations?

Example 12

medium
Trace: `a=20; b=12; while b!=0: a,b = b, a%b. print(a)`. Final a?

Example 13

medium
Trace: `n=27; steps=0; while n!=1: n = n//2 if n%2==0 else 3*n+1; steps+=1`. Is termination guaranteed for this code as written, and what is n's stopping value?

Example 14

medium
Trace: `i=0; count=0; while i<10: if i%3==0: count+=1; i+=1`. Final count?

Example 15

medium
Trace: `x=100; c=0; while x>1: x=x//2; c+=1`. Final c?

Example 16

medium
Trace: `i=5; while i>0: print(i); i-=2`. What prints?

Example 17

medium
Trace: `i=0; result=1; while i<5: result*=2; i+=1`. Final result?

Example 18

medium
Trace: `q=[3,1,2]; out=[]; while q: out.append(q.pop(0))`. Final out?

Example 19

medium
Trace: `i=0; while True: if i>=3: break; print(i); i+=1`. What prints?

Example 20

challenge
Bug hunt: `i=0; while i<5: print(i)` vs `i=0; while i<5: print(i); i=i+1`. Which terminates and why, and how many lines does the terminating one print?

Example 21

challenge
Trace: `i=1; j=10; while i<j: i+=2; j-=1; print(i,j)` give the final i and j and iteration count.

Example 22

challenge
Trace: `n=5; result=0; i=1; while i<=n: result+=i*i; i+=1`. Final result, and what formula does it compute?

Example 23

easy
Trace: `i=0; while i<2: print(i); i+=1`. What prints?

Example 24

easy
How many times does the body run? `i=0; while i<6: i+=1`.

Example 25

easy
Does this loop body execute even once? `x=10; while x<5: x-=1`.

Example 26

easy
Trace: `n=5; while n>0: n-=1; print(n)`. What prints?

Example 27

easy
Spot the bug: `n=3; while n>0: print('hi')`. What goes wrong?

Example 28

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

Example 29

medium
Trace: `i=2; while i<=16: i*=2`. Final i and iteration count?

Example 30

medium
Trace: `n=100; c=0; while n>1: n=n//2; c+=1`. Final c?

Example 31

medium
Trace: `i=0; while i<5: i+=2; print(i)`. What prints?

Example 32

medium
Trace: `a=48; b=18; while b!=0: a,b = b, a%b`. Final a?

Example 33

medium
Trace: `i=1; p=1; while i<=4: p*=i; i+=1`. Final p?

Example 34

medium
Trace: `s='hello'; i=0; out=''; while i<len(s): out+=s[i].upper(); i+=1`. Final out?

Example 35

medium
Trace: `i=10; while i>1: i//=2; print(i)`. What prints?

Example 36

medium
Trace: `n=7; sum=0; while n>0: sum+=n%10; n=n//10`. Final sum?

Example 37

medium
Trace: `n=123; sum=0; while n>0: sum+=n%10; n=n//10`. Final sum?

Example 38

medium
Trace: `a=0; b=1; while a<10: a,b = b, a+b`. Final a?

Example 39

medium
Trace: `i=0; while i<5: if i==3: break; print(i); i+=1`. What prints?

Example 40

medium
Trace: `i=0; while i<5: i+=1; if i%2==0: continue; print(i)`. What prints?

Example 41

medium
Trace: `x=1; y=0; while x<=4: y+=x*x; x+=1`. Final y?

Example 42

hard
Trace: `n=20; r=0; while n>0: if n%2==1: r+=1; n=n//2`. Final r?

Example 43

hard
Trace: `n=16; r=0; while (1<<r) < n: r+=1`. Final r?

Example 44

hard
Trace: `lo=0; hi=10; target=7; arr=[1,3,5,7,9,11,13,15,17,19]; while lo<hi: m=(lo+hi)//2; if arr[m]<target: lo=m+1; else: hi=m`. Final lo?

Example 45

hard
Trace: `n=6; a=0; b=1; i=2; while i<=n: a,b = b, a+b; i+=1`. Final b?

Example 46

challenge
Trace: `n=100; r=0; while n: r = r*10 + n%10; n//=10`. Final r?

Example 47

challenge
Bug analysis: `i=0; while i<5: print(i); i+=1; i-=1`. Does this terminate? If not, why?

What to focus on

Core idea: While loops check the condition before each iteration. If the condition is false from the start, the body never executes.

Common stuck point: Forgetting to update the loop variable creates an infinite loop. Always make sure the condition will eventually become false.

Ask yourself: Must the steps repeat until a condition changes, with the number of passes unknown before the loop starts?

Concept recap

A control structure that repeats a block of code as long as a specified condition remains true. The condition is checked before each iteration, and if it is false from the start, the loop body never executes at all.

Stirring a sauce until it thickens is a while loop: check whether it has thickened, stir if not, check again, and stop the moment it has. If it was already thick, you would not stir once. The comparison breaks in one place: a cook eventually gives up, but a loop whose condition never changes keeps going forever.

Read the full lesson on While Loop

Related concepts

Background knowledge

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

iteration