A while loop evaluates its condition before each iteration. If the condition is false initially, the body does not run. If it is true, the body runs and Python evaluates the condition again.
water = 2
capacity = 5
while water < capacity:
water += 1
print(water)
print("Final:", water)
3
4
5
Final: 5
The values progress from 2 to 5. At 5 the condition is false, so execution moves past the block. A condition that never becomes false can create an infinite loop. In a local terminal, Ctrl+C normally interrupts a running Python program with KeyboardInterrupt; interruption is not a replacement for designing a stopping condition.
A loop may depend on more than one changing condition. Here filling stops either at capacity or when supplies run out:
water = 2
capacity = 5
bottles = 2
while water < capacity and bottles > 0:
water += 1
bottles -= 1
print(water, bottles)
4 0
Both conditions start true. After two passes there are no bottles, so the second condition is false even though the container is not full. The example assumes each bottle adds exactly one unit and inputs are valid. A for loop is often convenient for a known sequence; while is useful when continuing depends on a changing condition.