Hey!
Before we start — let me give you a small puzzle.
A frog is stuck at the bottom of a 50 feet tall tank.
Every day — it jumps up 2 feet.
Every night — it slides back down 1.25 feet.
How many days does it take for the frog to escape the tank?
Take a guess. Write down your answer.
We are going to solve this with Python loops. And by the end — you will understand loops better than most beginners do.
Let us start.
1. What Is a Loop?
A loop is a way to run the same code again and again — until a condition is met.
Think of it like an alarm clock.
It keeps ringing. Every minute. Every minute. Every minute. Until you wake up and turn it off.
That "until you turn it off" part — that is your condition.
In Python — there are two types of loops.
-
forloop — runs a fixed number of times -
whileloop — runs until a condition becomes false
2. The for Loop
A for loop runs for a specific number of times. You know the count before it starts.
for i in range(5):
print(i)
Output:
0
1
2
3
4
range(5) gives numbers from 0 to 4. Five numbers. Loop runs five times.
Quick question for you.
What do you think range(1, 6) gives?
for i in range(1, 6):
print(i)
Output:
1
2
3
4
5
range(start, stop) — starts at 1, stops before 6. So 1 to 5.
3. The while Loop
A while loop keeps running as long as a condition is true. You do not need to know the count upfront. It just keeps going until something changes.
count = 0
while count < 5:
print(count)
count += 1
Output:
0
1
2
3
4
while count < 5 — keep running as long as count is less than 5.
count += 1 — increase count by 1 every time. Without this — the loop runs forever.
Quick question for you.
What happens if you forget to write count += 1?
The count stays 0 forever. 0 < 5 is always true. The loop never stops. That is called an infinite loop. Your program hangs.
Always make sure your while loop has a way to eventually become false.
4. break and continue — Two Useful Keywords
break — stops the loop immediately.
for i in range(10):
if i == 5:
break
print(i)
Output:
0
1
2
3
4
When i hits 5 — break exits the loop. 5, 6, 7, 8, 9 never print.
continue — skips the current step and goes to the next one.
for i in range(6):
if i == 3:
continue
print(i)
Output:
0
1
2
4
5
When i is 3 — continue skips it. The loop continues from 4.
5. Now — The Frog Problem
Let us read it again carefully.
- Tank height: 50 feet
- Frog starts at: 0 feet
- Day jump: +2 feet
- Night slide: -1.25 feet
- Question: How many days to escape?
Before you see the code — think about this.
What kind of loop should we use here — for or while?
We do not know how many days it will take. We keep going until the frog escapes.
That means — while loop. We run until the frog's position reaches 50 feet.
6. The Python Code
tankHeight = 50
nightSlide = 1.25
dayJump = 2
day = 0
while tankHeight > 0:
tankHeight = tankHeight - dayJump + nightSlide
day += 1
print(day)
Clean. Simple. Short.
Let us read it line by line.
tankHeight = 50 — the tank starts at 50 feet. This is the remaining height the frog needs to cover.
nightSlide = 1.25 — how much the frog slides back every night.
dayJump = 2 — how much the frog jumps every day.
day = 0 — day counter starts at zero.
while tankHeight > 0 — keep looping as long as there is tank height remaining.
tankHeight = tankHeight - dayJump + nightSlide — every loop reduces the remaining height. Frog jumps 2 feet up — so we subtract 2. But slides 1.25 feet back — so we add 1.25 back. Net reduction per day = 0.75 feet.
day += 1 — count the day.
print(day) — when the loop ends, print how many days it took.
Quick question for you.
Why do we write - dayJump + nightSlide and not - dayJump - nightSlide?
Because tankHeight is the remaining distance. When the frog jumps up — remaining distance goes down. So we subtract dayJump. When the frog slides back — remaining distance goes up again. So we add nightSlide back.
Subtracting progress. Adding back the slide. That is the logic.
7. What Is the Output?
Run it and see.
49
That is it. The frog escapes on day 49.
No extra print statements. No step-by-step logging. Just the answer — clean and direct.
Quick question for you.
Did you guess 49 at the start?
Most people guess something between 30 and 45. Because they think — "50 divided by 0.75 is about 66." But that formula is wrong. The frog does not slide back on the last day. So the actual answer is less.
The loop handles this correctly because it checks tankHeight > 0 after each full day including the slide. When the tank height goes to 0 or below — the loop stops and we count that day.
That is the beauty of loops. No formula needed. Just describe what happens each day — and let the loop repeat it until the condition is met.
8. What Did We Learn About Loops From This?
The frog problem taught us something important.
You do not always know how many times a loop will run.
Sometimes you just know the condition — "keep going until the frog escapes." That is a while loop.
Sometimes you know the exact count — "do this 10 times." That is a for loop.
And break is your emergency exit — when something happens mid-loop and you need to stop immediately.
In the frog problem — break is the moment the frog jumps out. We do not let it fall back. We stop right there.
Quick Summary — 5 Things to Remember
for loop — use when you know how many times to repeat.
for i in range(n).while loop — use when you do not know the count. Keep running until a condition is false.
break — exits the loop immediately. Used when the goal is reached mid-loop.
continue — skips the current step and moves to the next one.
Always have an exit condition in while loops — otherwise your loop runs forever and your program hangs.
Loops are everywhere in programming. Search results. Payments. Games. Animations. Any time something needs to repeat — there is a loop behind it.
The frog problem is a great way to practice because it has a real-world feel. Days passing. A goal to reach. A condition to check each step.
Try changing the numbers. Make the tank 200 feet. Make the frog jump 5 feet but slide back 4.9 feet. See how many days it takes.
Then try writing it without looking at the code above.
That is when you know loops have clicked.
If you have a question — drop it in the comments below.
Thanks for reading. Keep building.

Top comments (0)