Computer Science · Ch 6 — Flow of Control
The ‘While’ Loop
The ‘While’ Loop
The while statement executes a block of code repeatedly as long as the control condition of the loop is true. Unlike the for loop, which walks a known sequence, a while loop keeps going purely on the strength of its condition.
How a while loop executes
- The control condition is evaluated before any statement inside the loop runs.
- After each iteration, the condition is tested again; the loop continues while it remains true.
- When the condition becomes false, the body is not executed and control transfers to the statement immediately following the body of the
whileloop. - If the condition is false at the very start, the body is not executed even once.
- The statements inside the body must ensure the condition eventually becomes false; otherwise the loop becomes an infinite loop — a logical error in the program.
The flowchart of the while loop is shown in Figure 6.5: initialisation, then the test expression, then (if true) the body, looping back to the test; when the test is false, control moves to the statements following the loop.
Syntax
while test_condition:
body of while
The body of the loop is indented with respect to the while statement.
Counting with a while loop
The first five natural numbers, printed with a control variable that is initialised, tested and incremented explicitly:
# Print first 5 natural numbers using a while loop
n = 1
while n <= 5:
print(n)
n += 1
1
2
3
4
5
Trace the pattern: n starts at 1 (initialisation), n <= 5 is the test, and n += 1 inside the body is what guarantees the condition eventually turns false. Remove that increment and this becomes an infinite loop.
Finding the factors of a number
A while loop works naturally when candidate values are tried one after another. A factor of a whole number divides it exactly (remainder 0 with the % operator). Since 1 divides every number and every number divides itself, those two factors can be printed without testing; only the candidates from 2 up to half the number need checking — no factor other than the number itself can exceed its half:
# Find the factors of a whole number
number = int(input("Enter a number to find its factor: "))
print(1, end=' ') # 1 is a factor of every number
trial = 2
while trial <= number / 2:
if number % trial == 0:
# end=' ' makes print use a blank space as the delimiter,
# so the next value appears on the same line
print(trial, end=' ')
trial += 1 …
Drawn by us to help you understand the concept clearly, and verified to make sure it's accurate. For exams, practice from your NCERT textbook's own diagram.
This figure (printed as Figure 6.5) is the flowchart of how a while loop executes. (Note: the book's printed numbering assigns "Figure 6.5" twice — this while-loop chart and the break-statement chart in Section 6.5.1 share the number; an NCERT misprint.)
Reading it from the top:
- An oval terminator "Start" leads down into a rectangle: the Initialisation Statement. This runs once, before the loop — typically setting the loop's control variable (for example,
count = 1). - The arrow continues down to a diamond labelled "Test Expression" — the loop's control condition. On the arrow above the diamond there is a junction where the loop-back arrow from the left rejoins the flow.
- If the test is True, the flow continues downward into a rectangle: the Body of while Loop. Its statements execute once.
- From the body, an arrow goes left and then up, re-entering the flow line above the test expression. This is the loop back: after every pass through the body, the condition is evaluated again, and the loop continues for as long as it stays true.
- If the test is False, an arrow leaves the diamond to the right into a rectangle "Statements following the while loop", and then down to the terminator "Stop". …