This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: While loops

While loops

face Josiah Wang

Now we move on to the third basic building block, which is the repetition block.

The repetition block

We discussed the while loop in Lesson 1.

What does the following code do? Try to guess first before running it to confirm.

n = 10
while n > 0:
    print(n)
    n = n - 1
print("Happy new year!!")

Also remember that you can nest another while statement (or any building block) inside a loop.

Nested repetition block