1.Mastering while Loops in Python with Real-Time Examples
Programming becomes interesting when we connect logic with real-world situations.
Instead of learning loops with boring numbers, let’s understand Python while loops using practical examples like:
Finding railway stations
Detecting common multiples
Catching a thief
In this blog, we’ll break down multiple Python programs step-by-step and understand how loops actually work behind the scenes.
2.Example 1 — Finding Numbers Divisible by 3 and 5
Program
station_no = 1
while station_no <= 30:
if station_no % 3 == 0 and station_no % 5 == 0:
print(station_no)
station_no = station_no + 1
Output
15
30
Explanation
Here:
station_no starts from 1
The loop runs until 30
We check:
station_no % 3 == 0
AND
station_no % 5 == 0
If both are true, the number is printed.
3.What Does % Mean?
% is called the modulus operator.
It returns the remainder.
Example:
15 % 3 = 0
Because 15 is perfectly divisible by 3.
4.Example 2 — Finding the First Matching Station using break
Program
station_no = 1
while station_no <= 300:
if station_no % 3 == 0 and station_no % 5 == 0:
print(station_no)
break
station_no = station_no + 1
Output
15
5.What is break?
The break statement in Python is used to exit or "break" out of a loop (either for or while loop) prematurely, before the loop has iterated through all its items or reached its condition. When the break statement is executed, the program immediately exits the loop, and the control moves to the next line of code after the loop.
The break keyword is used to break out a for loop, or a while loop.
Without break, the loop would print:
15
30
45
60
...
But with break, the loop stops after finding the first matching value.
6.Example 3 — Unknown Number of Stations using while True
Program
station_no = 1
while True: # Total no. of stations unknown
if station_no % 18 == 0 and station_no % 62 == 0:
print(station_no)
break
station_no = station_no + 1
Output
558
Why while True?
Sometimes we don't know:
how many times the loop should run
where the answer exists
So we create an infinite loop using:
while True:
Then stop it manually using break.
7.Example 4 — Police Catching a Thief
Program
while True:
police = police + 5
thief = thief + 2
if police >= thief:
print(police)
break
Concept Behind the Program
This is a beautiful real-world simulation.
Police speed increases by 5
Thief speed increases by 2
The loop runs continuously.
Once:
police >= thief
The thief gets caught.
Important Note
Before running this program, initialize the variables.
Example:
police = 0
thief = 20
Complete Program:
police = 0
thief = 20
while True:
police = police + 5
thief = thief + 2
if police >= thief:
print(police)
break
Output
35
8.Why These Programs Matter
Most beginners think loops are difficult.
But once you connect loops with:
trains
stations
police chase
real-time logic
Programming becomes much easier and more fun.
9.Final Thoughts
The while loop is one of the most powerful concepts in Python.
Whether you're:
searching for values
building games
creating simulations
solving real-world problems
while loops are everywhere.
The best way to master loops is:
Practice
Visualize
Build logic gradually
Because programming is not about memorizing syntax —
it’s about training your thinking process.
Reference
(https://www.geeksforgeeks.org/python/python-break-statement/)
Top comments (0)