Loops, and where your data lives

INPUT · Slides

The loop that never ends

01 / 07

A program that will not stop

In the while lesson we said that forgetting the advancing line means it never finishes. Here is what that actually looks like.

number stays at 0 for ever, so number < 3 holds for ever. The same line simply streams past, on and on.

number = 0while number < 3:    print(number)

Result

0
0
0
and on from here, until the course runtime cuts the repeating off

02 / 07

Why does it not end

while ends only when the condition stops holding. Which means, to end it, a value in the condition has to change.

The only thing in the condition above is number, and nothing anywhere changes number. Nobody is going to bring it to an end.

When you see code that will not stop, ask this first: "who is changing the value in the condition?"

03 / 07

Three causes cover most of it

An endless loop almost always comes from one of these.

  • the advancing line was never written … no number += 1 anywhere
  • the advancing line is outside the loop … not indented far enough
  • it is advancing the wrong way … moving away from the end

All three show themselves once you follow how the value in the condition moves. Do not run it in your head — walk one go at a time with your finger.

04 / 07

Too little indent puts it outside

Even if you did write it, too little indent leaves it outside the loop. From inside, that is the same as not writing it at all.

In the example below number += 1 has slipped back to the left margin. Python raises nothing. What you have is a program that is grammatically perfect and never finishes.

number = 0while number < 3:    print(number)number += 1

Result

0
0
0
and on from here, until the course runtime cuts the repeating off

05 / 07

The wrong direction never ends either

while number > 0: says "carry on while number is above 0". Increase number there and the condition only holds more firmly.

The advancing line is properly inside, and still it will not end. Hold the direction against the condition and check that it moves towards the finish.

number = 1while number > 0:    print(number)    number += 1

Result

1
2
3
and on from here, until the course runtime cuts the repeating off

06 / 07

Do not be shy — make one on purpose

The runtime in this course is set up to cut the repeating off automatically after a while. So you can get it wrong here in complete safety.

Endless loops are not the preserve of beginners. People who write code for a living produce them as a matter of course. The only difference is whether they can fix them.

Once you can explain in your own words why it does not end, you have understood it. Which is why every exercise in this lesson is mend the code that will not stop, and make it stop.

07 / 07

The order to check things in

When you meet code that will not stop, work down this list.

  • check the name of the variable in the condition
  • find the line that changes that variable
  • look at the indent: is that line inside the loop?
  • check that the direction it moves in is the one that breaks the condition

Pass all four and the loop is certain to end. Being able to satisfy yourself that it ends before you run it is where the fear goes.