Learn how to repeat blocks of code as long as a condition remains true.
Imagine you are a robot tasked with cleaning a room. You don't know exactly how many toys are on the floor, but you know you must keep picking them up while there is still a mess. How do you tell a computer to keep going until the job is done?
A while loop is like a gatekeeper standing in front of a block of code. Before letting the code run, the gatekeeper checks a condition. If the condition is True, the code inside the loop runs. Once the code finishes, the computer jumps back to the top and asks the gatekeeper again: 'Is the condition still True?' This cycle continues until the condition finally becomes False. In Python, we write this using the `while` keyword followed by a comparison, such as . It is the perfect tool for when you don't know exactly how many times you need to repeat something, but you know exactly when you should stop.
Let's look at a simple countdown script: 1. We start with a variable . 2. The loop checks: Is ? 3. Since is True, it prints the number. 4. We subtract 1: . 5. The loop repeats until reaches 0, making the condition False.
Quick Check
If the condition of a while loop is False the very first time the computer checks it, how many times will the code inside run?
Answer
Zero times.
To keep a loop from running forever, we often use a loop counter. This is a variable that tracks how many times the loop has executed. Every time the loop runs, we increment (add to) or decrement (subtract from) that variable. For example, if we want to track how many steps a character takes, we might use . Without this change, the condition would stay True forever! This variable acts as the 'heartbeat' of your loop, ensuring that progress is being made toward the exit condition.
Imagine you find a bag that doubles your gold every minute, but it stops when you have more than 100 coins. 1. Start with . 2. Set the condition: `while gold <= 100:` 3. Inside the loop, update the gold: . 4. The loop will run for values 1, 2, 4, 8, 16, 32, 64, and stop once it hits 128.
Quick Check
What is the term for a variable used to track and change the loop condition so it eventually ends?
Answer
A loop counter (or iterator).
The most common mistake in programming is the infinite loop. This happens when the condition never becomes False. If you tell a computer `while 1 < 2: print('Hello')`, it will print 'Hello' forever because 1 is always less than 2! This can cause your program to freeze or crash. To avoid this, always double-check that the variables inside your condition are being updated correctly inside the loop body. If your condition is , make sure is actually getting larger each time the loop runs!
Let's simulate a vault that requires a passcode. 1. Set `entered_code = ''` and `correct_code = '1234'`. 2. Set a limit: . 3. Use a loop: `while entered_code != correct_code and attempts < 3:` 4. Inside, ask for the code and update . 5. This loop uses two conditions to ensure the user doesn't get infinite guesses!
Which of the following is required to prevent a while loop from becoming an infinite loop?
If you have and your loop is `while i < 4:`, and inside you have , how many times will the loop run?
A while loop is the best choice when you know the exact number of times you want to repeat a task before you start.
Review Tomorrow
In 24 hours, try to explain to a friend the difference between a loop condition and a loop counter.
Practice Activity
Try this on your own: Write a while loop that starts at 10 and counts down to 0, but only prints the even numbers.