AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Contents · Computing

Loop control and swapping values

Lessons 46 · 1 lessons · M. R. Fayziyeva, D. M. Sayfurov, N. S. Xaytullayeva. Informatics and Information Technologies, Grade 9. “Nashriyot uyi Tasvir”, Tashkent, 2020
46

Controlling loops: the continue and break statements

Textbook: pp. 89–91
GoalTells the jobs of the break and continue operators apart, ends a while True: loop correctly with break, and swaps the values of two variables in the form a, b = b, a.
New words
break: leaves the loop at once · breakcontinue: skips the rest of the current pass and goes to the next iteration · continuewhile True: a loop that stops only by break · while Truea, b = b, a: swaps the values of two variables in one line · a, b = b, a
Explanation

The work of a loop can be controlled not only by the loop condition. The break operator ends the loop at once and the program continues from the line after the loop; it is usually written inside an if. continue throws away the rest of the current iteration and goes to the next turn of the loop, and the loop does not stop. For example, to accept words until the user types “stop”, while True: is written and break is done when “stop” comes: here the number of repetitions need not be known beforehand. A separate topic is swapping values. In other languages a third variable is needed, but in Python it is enough to write a, b = b, a: first the right side is calculated and then the values are given to the names on the left. So in a, b = b, a + b both expressions are calculated with the old values.

What it does
breakends the loop at once
continueskips the rest of the current iteration and goes to the next
💡 Where will I use this?
In real life

A pupil looking for their own name on the printed list for a trip stopped reading as soon as the name was found. There was no need to read the other lines, which is the idea of break.

Who uses it

Programmers, librarians and search staff stop checking when the needed item is found or skip unneeded items.

Try it today

Write a list of ten numbers on paper and stop at the first one greater than 7. At which step did you stop, and why?

Worked examples
Python
1while True:
2    w = input()
3    if w == "stop":
4        break
5    print(len(w))
Output
4
5
Stopping with break – code above. If olma, kitob and stop are typed, 4 and 5 are printed; when stop comes the loop ends and its length is not printed.
Python
1for i in range(1, 11):
2    if i % 3 == 0:
3        continue
4    print(i, end=" ")
5
6a, b = 1, 2
7a, b = b, a
8print(a, b)
Output
1 2 4 5 7 8 10
2 1
Skipping with continue – the first program above. Multiples of 3 are not printed: 1 2 4 5 7 8 10. Swapping – the second program above. 1 and 2 change places: 2 1.
Class activity

“Exchange”: two pupils show a, b = b, a live with cards (values) in their hands; then they carry out a, b = b, a + b three times in a row and watch the result.

Practice
1
What does this program print?
Python
1for i in range(1, 8):
2    if i == 5:
3        break
4    print(i, end=" ")
2
Write a program with continue that finds the sum of the numbers from 1 to 20 that are not multiples of 5.
3
After a, b = 3, 9, if a, b = b, a + b is carried out, what are a and b?
4
Why must a while True: loop contain a break?
Story of the Week · The Khwarizmi WayThe fair standClose the week: all of this week’s subjects in one storyRead →