Course 4.2Lesson 2 of 5
break leaves a loop at once, and the program continues with the line after the loop. continue skips the rest of the current round and starts the next one. Both are used with an if inside the loop. while True with a break inside is a loop that stops from the middle, and is useful when the stopping test comes in the middle of the body. A sentinel is a special value, such as 0, that means stop.
Try the code and the simulations beside the lesson.
After this lesson you can
break,continue,while True loop that stops from the middle, andSometimes you find what you are looking for in the middle of a loop. Then there is no need to go on. Imagine that you look for a lost key in ten drawers. When you find it in the third, you do not open the other seven.
break leaves the loop at once. The program goes on with the line after the loop.continue skips the rest of this round and goes on with the next round.Both are used with an if inside the loop, so that they happen only in a special case.
break works in both kinds of loops, for and while.
A caution for while loops: if the line that changes the condition comes after the continue, the continue skips it, and the loop may never end. In a while loop, change the variable before the continue.
Put the continue number after the break number. It never gets its turn, because the loop has already ended.
The condition True is always true. So while True: is an endless loop, unless a break stops it. It looks strange, but it is useful when the stopping test comes in the middle of the body, and not at the top.
Every while True loop must have a break inside it, in a way that will really happen. When you write one, ask yourself: "How does this loop end?"
A sentinel is a special value that means "stop". You ask for numbers until the user types 0, for example. The sentinel is not part of the data. It is only the sign of the end. You will meet this in the next lesson, with input().
Both can end a loop. When the reason to stop is simple, put it in the condition: while savings < 500. When the reason comes in the middle of the body, or there are several different reasons, use while True and break. Choose the one that is easier to read.
break outside a loop. It is a SyntaxError.continue that skips the line that moves the condition, in a while loop. The loop never ends.break when you meant continue. One leaves the loop, the other only skips one round.while True with no break that can really happen.break leaves only the loop that it is in, not an outer one.Start with two quick questions. Each New numbers gives you a fresh one, so you can practise as long as you like.
Then the labs and the problems:
break to stop at the right number.while True and break.break leaves the loop now. continue skips to the next round.if.while True needs a break that can happen.while loop, change the variable before a continue.Try each one on your own first. If you are stuck, open a hint. Then check with Show the answer.
break do?for n in range(1, 10): if n == 5: continue; print(n) show?while True loop end?Every lesson is open to read. Enrolling also lets you:
Log in to join the discussion.
Loading…
This lesson is free to read. Enroll free to keep your place and take the tests.