While Loop Examples in CS Thinking

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

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

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.

A while loop is like 'keep going until...' β€” keep stirring the soup WHILE it's not boiling. Keep asking for a password WHILE the password is wrong.

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

Sense of Study hint: When writing a while loop, first initialize the variable that the condition tests. Then write the condition that should keep the loop running. Inside the loop body, make sure to update the variable so the condition eventually becomes false. Trace through mentally with a small example.

Worked Examples

Example 1

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

Answer

Output: 128. 7 iterations.

First step

1
Step 1: Start at n=1n = 1 and double each time: 1β†’2β†’4β†’8β†’16β†’32β†’64β†’1281 \to 2 \to 4 \to 8 \to 16 \to 32 \to 64 \to 128.

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

SetupKey insightWhy it worksCommon pitfallConnection

Unlock answer keys One Family plan β€” every worked solution, all subjects

Example 2

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

Practice Problems

Try these problems on your own first, 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 ii and iteration count?

Example 30

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

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 aa?

Example 33

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

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 aa?

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 yy?

Example 42

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

Example 43

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

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 bb?

Example 46

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

Example 47

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

Related Concepts

Background Knowledge

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

iteration