Career upgrade: Learn practical AI skills for better jobs and higher pay.
Level up

While Loops and Sentinel Patterns

Key Takeaways

  • A while loop checks its condition before every iteration, including before the first possible run.
  • Every terminating while loop needs some path that changes the condition or executes break.
  • Sentinel loops use a special value to stop input or processing without treating that value as normal data.
  • Counters, accumulators, and flags should be updated in a deliberate order because one misplaced line changes the result.
  • PCEP while-loop traps include off-by-one boundaries, skipped updates after continue, and accidentally processing the sentinel.
Last updated: May 2026

How a while loop runs

A while loop repeats while its condition is truthy. Python checks the condition before entering the loop and checks it again after each completed iteration.

n = 1
while n < 5:
    print(n)
    n += 2

This prints 1 and 3. The loop does not print 5 because after n += 2, the next condition is 5 < 5, which is false.

The most important question for any while loop is: what changes? If the condition can remain true forever, the loop is infinite. In exam snippets, the update may be at the bottom, inside a branch, or accidentally skipped by continue.

Counters and accumulators

A counter tracks how many times something happened. An accumulator builds a total, string, or collection.

count = 0
total = 0

while count < 3:
    count += 1
    total += count

print(total)

Trace it row by row:

Iterationcount before bodycount after += 1total after add
1011
2123
3236
stop3condition false6

The answer is 6, not 3, because the loop adds the updated counter each time.

Sentinel pattern

A sentinel is a special value that means stop. It is not normal data. Common sentinels include 'quit', 'done', '', 0, or -1, depending on the problem.

total = 0
value = int(input())

while value != -1:
    total += value
    value = int(input())

print(total)

If the input is 4, 7, -1, the total is 11. The sentinel -1 is not added. A classic PCEP trap is placing total += value before checking whether value is the sentinel.

A second sentinel style uses while True and break:

total = 0
while True:
    text = input()
    if text == 'done':
        break
    total += int(text)

This is readable when the stop test naturally belongs in the middle of the loop.

Trace-table method

For while loops, create columns for every variable that changes:

Checkconditioninput/valueactiontotal
1value != -14add4
2value != -17add11
3value != -1-1stop11

Include a final stop row. Many off-by-one mistakes disappear when you write the condition that fails.

Flags

A flag is a boolean variable that records whether something has happened. For example, found = False can become True when a matching item appears. In a while condition, a flag is often combined with a counter, such as while index < len(data) and not found:. Trace the flag separately from the counter so you can see whether the loop stopped because the search succeeded or because the data ended.

Exam traps

First, remember that the loop may run zero times if the initial condition is false. Second, input() returns a string, so numeric comparisons require conversion with int() or float(). Third, continue jumps back to the condition check, so any update below continue is skipped. Finally, be precise about < versus <=; that one character often decides the number of iterations.

Test Your Knowledge

What is printed by this code? n = 2 while n <= 8: n += 3 print(n)

A
B
C
D
Test Your Knowledge

A sentinel value in a loop is best described as:

A
B
C
D
Test Your Knowledge

What is the main risk in this loop? n = 0 while n < 3: if n == 1: continue n += 1

A
B
C
D