Introduction:
A while loop is a type of loop in Python that keeps repeating a block of code as long as a certain condition remains true. It’s like a set of instructions that keeps going until something tells it to stop.
Imagine you're playing a video game. You keep playing as long as your health is above zero. When your health drops to zero, the game stops. In the same way, a while loop keeps running until a certain condition is no longer true.
How is a While Loop Set Up?
A while loop is set up like this:
count = 5 while count > 0: print(count) count = count - 1
Here’s what each part means:
- count = 5: This is where we set an initial value. In this case, we're starting with
count
set to 5. - while count > 0: This is the condition. The loop will keep running as long as
count
is greater than 0. - print(count): This is the action we want to repeat. The loop will print the value of
count
every time it runs. - count = count - 1: This is where the value of
count
decreases by 1 each time the loop runs. This is important because without it, the loop would never end!
The loop will print the value of count
starting from 5, and then decrease the value of count
by 1 each time.
So it prints:
5
4
3
2
1
After that, the loop stops because the condition count > 0
is no longer true.
What is the Purpose of a While Loop?
The purpose of a while loop is to repeat a task until a certain condition changes. Unlike a for loop (which repeats a set number of times), a while loop doesn’t have a fixed number of repetitions. It keeps going until the condition becomes false.
How is a While Loop Different from a For Loop?
- For Loop: You know how many times it will run. For example, "repeat 10 times." It’s great when you know exactly how many times you want something to happen.
- While Loop: It will run as long as a condition is true. You don't know in advance how many times it will run. It’s perfect when you need the loop to continue until something specific happens, but you're not sure when that will be.
Real-World Examples
- Video Games: In a game, you might have a while loop that keeps running while the player’s health is greater than zero. When the health hits zero, the game stops.
- Password Attempts: Imagine you're trying to log into your phone, but you only get three attempts before it locks. A while loop could check how many attempts you’ve used and stop when you hit three.
- Waiting for a Condition: Let's say you're baking a cake. You want to check if it’s baked enough every minute. You’d use a while loop to keep checking until the cake is done.
Quick Analogy
Think of a while loop like running a race. The race keeps going while you're still running, but once you cross the finish line, the race stops. Similarly, the while loop keeps running while the condition is true, but once the condition becomes false, the loop ends.
They are super useful when you don't know exactly how many times a task needs to be repeated, but you know when to stop once a condition changes.
Predict:
Take a look at the following code. Before you run it, can you predict what will happen?
What do you think will be printed, and how many times will it print? Think about it for a moment before continuing.
number = 3 while number > 0: print("This is fun!") number = number - 1
Run:
Now let’s run a simple while loop. This code will keep printing numbers until the condition is no longer true:
Run this code in the embedded IDE to see how a while loop works. The loop will print the numbers 4, 3, 2, and 1, and then it will stop because the condition counter > 0
is no longer true.
Investigate:
Let’s dig a little deeper. Now, we'll change the condition of the while loop slightly. Run the following code and pay attention to how the loop behaves:
What do you notice?
This time, the loop starts at 0 and keeps going as long as x < 5
. It prints the value of x
and then increases x
by 1 each time. Try experimenting by changing the condition to see how that changes the loop’s behaviour.
Modify:
Here’s some code that is meant to count down from 3 to 1, but there is a mistake in it. Can you fix the error so that the code works correctly?
Hint: The loop isn’t reducing the value of n
, so it will run forever. You need to modify the code to reduce n
each time, so the loop eventually stops.