While Loops
Earlier we looked at using for loops to loop through a block of code repeatedly. Python has another sort of loop called a While loop.
A while loop is a bit like a repeated if statement. It says that while a certain condition is true do something until it is not true.
Here is a piece of code for you to try out:

Follow through each line carefully and try to understand what it does. Remember to read the last line as counter becomes counter plus one.
Now try the following:

Did you see any difference in the output? Can you tell why?
![]() |
|
Thinking About Loops
All loops need a beginning, middle and end. When you are setting up your own loops you should think about the following things:
1. Initialising Variables:
- Are there any variables (eg counters) that need to be set up before the loop begins?
2. Inside the Loop:
- What instructions do we want to repeat? Take care to only include instructions that need to be performed repeatedly inside the body of the loop.
- What things need to change inside each loop? For example you might need to increment a counter.
3. Ending the Loop:
- How will the loop end? How will the computer know when to stop?
![]() |
|




