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:
print('hello')
print('hello')
print('hello')
print('hello')
print('hello')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:
Drag it up. One of these two programs has to be rewritten every time you change your mind.
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.
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.
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.
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 onceWhat 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.
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?
| Loop | Use it when | For example |
|---|---|---|
for | the number of repetitions is known beforehand | the table of 7 — always 10 rows |
while | the number of repetitions is not known beforehand | asking 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.
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.
5Recap
for, when the number of repetitions is known before the loop starts, and while, when it is not.What is the main reason to use a loop instead of repeating a statement by hand?
A program must print every name in a list of 500 students. Which loop fits naturally?
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?