While loops
Syntax
x |
= |
1 |
while |
x<=10 |
: |
|
print x |
|
|
x = x + 1 |
|
About While
While loops allow you to repeat code. In order to repeat code you need to know when the code should start, when it should end and how many steps it must take from start to end. This is an over simplification but it helps when first starting out with loops.
Here is the code from the video –
x = 100 while x>=1: print x x = x - 1
Key term
Iteration – The process of looping over a section of code. A single loop is known as an iteration.
Helpful links
Some sample code for while loops
Kahn academy – while loops (video)
Using boolean variables and while loops (example)
Test your skill
1. Display the numbers from 20 to 30.
2. Display the first 20 square numbers (e.g. 1×1, 2×2,3×3, 4×4….)
3. Try the task even stevens on the HackIT website.
4. Try the task 99 bottles of beer on the HackIT website.











