LambdaLabTM
Computer Science · Class 11 · Iterative Statements
Types of StatementsIteration⏱️ 9 min read

What is an Iterative Statement?

A selection statement lets a program skip lines. An iterative statement lets it run the same lines again and again. It is also called a repetitive or looping statement, and it is the reason ten lines of Python can handle ten thousand marks.

1The problem with doing it by hand

Print hello five times. With what you know so far, that is five statements:

five_hellos.py
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')
Output
hello
hello
hello
hello
hello

It works. Now print it five hundred times, and the program becomes five hundred lines that all say the same thing. Drag the dial below and watch which of the two programs has to be rewritten:

🔁 Print ‘hello’ this many times

Drag it up. One of these two programs has to be rewritten every time you change your mind.

5
with a loop2 lines
for i in range(1, 6): print('hello')

One number changed. The program is the same length it was at 5, and it will be the same length at a million.

without one5 lines
print('hello')print('hello')print('hello')print('hello')print('hello')

Every change means editing all of them. Miss one and the program is wrong in a way nothing will report.

Key Takeaway
An iterative statement (a loop) tells Python to run a block of statements repeatedly. The block is written once and run many times, so the length of the program stops depending on the amount of work.

2The shape of a loop

A loop is written like the selection statements you have just met: a header ending in a colon, and an indented block beneath it. Nothing new to learn there — the indentation still marks which lines belong to the statement.

shape.py
for i in range(1, 6):    <- the header, ending in a colon
    print('hello')       <- the body: the block that repeats
print('Done')            <- back at the margin: outside, so it runs once

What is new is how often the block runs. With an if, the block runs once or not at all. With a loop, the same block runs over and over — and the lines below it, back at the margin, still run exactly once, after the loop has finished.

Note
The body of a loop is called an iteration. One trip through the block is one iteration, or one round. Papers use both words, and “how many iterations?” simply means “how many times did the block run?”

3Python has two loops

And there is one question that tells you which to write: do you know how many times it has to repeat?

LoopUse it whenFor example
forthe number of repetitions is known beforehandthe table of 7 — always 10 rows
whilethe number of repetitions is not known beforehandasking for a password until it is right

“Known beforehand” does not mean you know it while you are writing the program. It means the program knows it before the loop starts. A loop over a list of 500 students is a for loop even though you never counted them: by the time the loop begins, the list exists and its length is settled.

Waiting for a correct password is different. The number of tries is not settled at the start, not by you and not by the program — it depends on what happens inside the loop. That is a while.

Tip
Either loop can be made to do the other's job, and in an exam both may earn the marks. But the natural fit saves you writing: a for loop counts for you, while a while loop makes you start the counter and move it yourself — three chances to forget something instead of none.

4What comes next

Four lessons, in this order. First range(), which is not a loop at all but produces the numbers a for loop counts through — teaching for without it means asking you to accept half the line on trust. Then for itself, then while, and finally what happens when one loop is put inside another.

Note
Nothing about loops needs memorising today. This page has one idea in it: some statements are written once and run many times. The rest of this submenu is where you learn to write them.

5Recap

Key Takeaway
An iterative statement — a loop — runs a block of statements again and again. The block is the indented part under a header that ends in a colon. Python has two: for, when the number of repetitions is known before the loop starts, and while, when it is not.
Quick Check

What is the main reason to use a loop instead of repeating a statement by hand?

Quick Check

A program must print every name in a list of 500 students. Which loop fits naturally?

Quick Check

In 'for i in range(1, 6):' followed by an indented print and then a print at the margin, how often does the line at the margin run?