DEV Community

Cover image for From Railway Stations to Police Chase — Master Python While Loops
Hariharan S J
Hariharan S J

Posted on

From Railway Stations to Police Chase — Master Python While Loops

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
Enter fullscreen mode Exit fullscreen mode

Output

15
30
Enter fullscreen mode Exit fullscreen mode

Explanation

Here:

  • station_no starts from 1

  • The loop runs until 30

  • We check:

station_no % 3 == 0
Enter fullscreen mode Exit fullscreen mode

AND

station_no % 5 == 0
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output

15
Enter fullscreen mode Exit fullscreen mode

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
...
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Output

558
Enter fullscreen mode Exit fullscreen mode

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:
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

The thief gets caught.

Important Note

Before running this program, initialize the variables.

Example:

police = 0
thief = 20
Enter fullscreen mode Exit fullscreen mode

Complete Program:

police = 0
thief = 20

while True:

    police = police + 5
    thief = thief + 2

    if police >= thief:
        print(police)
        break
Enter fullscreen mode Exit fullscreen mode

Output

35
Enter fullscreen mode Exit fullscreen mode

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/)

(https://www.w3schools.com/python/ref_keyword_break.asp)

Top comments (0)