Leaving early
After this: Leave a loop with break and skip one pass with continue.
On this page · 4 sections · 3 checks
You are looking for the first even number in a row of numbers. Once you have found it, there is no reason to look at the rest. A loop as taught so far goes through every value. This lesson gives you two ways to change that: leave the loop at once, or skip the rest of one pass.
Leaving the loop: break
The word break ends the loop at once. The program moves on to the line after the loop, whatever values were left.
Work through the search for the first even number in 1, 3, 4, 5, which is in the trace below. A number is even when the remainder after dividing by 2 is 0, which is the condition n % 2 == 0. The box found is made before the loop, so that the answer is still there after the loop.
nis 1. 1 % 2 is 1, not 0, so the condition is false. Theifis skipped and "1 is odd" is printed.nis 3. The same: "3 is odd".nis 4. 4 % 2 is 0, so the condition is true.foundbecomes 4, and thenbreakends the loop.- The line after the loop prints "first even: 4". The value 5 was never looked at.
- Run the programRun it. The values 1 and 3 are odd and are printed. Watch what happens when n is 4.
found = 0for n in [1, 3, 4, 5]: if n % 2 == 0: found = n break print(n, "is odd")print("first even:", found)run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
In the trace, break shows as a jump to line 711, the line after the loop, with no jump back to the top.
Change the values so that none of them is even, and run again. What is printed at the end, and why?
Skipping one pass: continue
The word continue ends the current pass only. The rest of the loop's lines are skipped for this value, and the loop goes back to the top for the next value.
Count from 1 to 6, skipping the multiples of 3. A number is a multiple of 3 when i % 3 == 0.
iis 1, then 2: the condition is false, and each is printed.iis 3: the condition is true,continueruns, and the print line is skipped. The loop goes back to the top.iis 4, then 5: printed.iis 6: skipped. Then nothing is left, and Done is printed.
for i in range(1, 7): if i % 3 == 0: continue print(i)print("Done")run the program to see its boxes
nothing printed yetPress Run it, then step through the program one line at a time.
- lines
- 0
- prints
- 0
- calls
- 0
In the trace, continue is narrated as "The loop goes back to line 13", from the continue line itself, without reaching the print line.
The rule: break leaves the loop; continue skips to the next pass. Both are nearly always written inside an if, because leaving or skipping only makes sense when something has been found or ruled out.
Check yourself
The first-even program is run on the row [7, 9, 6, 8]{7, 9, 6, 8} instead. At which value does break end the loop?
What does this print?
for i in range(1, 6): if i % 2 == 0: continue print(i)for (int i = 1; i < 6; i++) { if (i % 2 == 0) { continue; } System.out.println(i);}Complete line 3 so that the loop stops at the first value bigger than 10 and puts it in found. The program should print 12. Write the whole line, including the colon at the endbrackets and the opening brace.
found = 0for n in [4, 12, 7]: ____________________ # ← is n bigger than 10? found = n breakprint(found)int found = 0;for (int n : new int[]{4, 12, 7}) { ____________________ // ← is n bigger than 10? found = n; break; }}System.out.println(found);Honesty notes
- Use break to leave a loop at once, usually inside an if, when the value you were looking for is found.
- Use continue to skip the rest of one pass and go on to the next value.
- Keep the answer of a search in a box made before the loop, so the program can use it after the loop.
- Say which value a search stops at: the first even number in 1, 3, 4, 5 is 4, and 5 is never looked at.