1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

break & continue

Two keywords steer a loop from the inside:

  • continue skips the rest of this iteration and jumps to the next one
  • break leaves the loop completely
for n in range(10):
    if n == 2:
        continue   # skip 2
    if n == 5:
        break      # stop at 5
    print(n)       # 0 1 3 4

The extra practice uses lists: square brackets hold values in order, and [] is an empty list. A for loop can read those values directly:

kept = []
for value in [2, -1, 5]:
    if value < 0:
        continue
    kept.append(value)   # add one value to the end
# kept is [2, 5]

See it run

words = ["hi", "", "yo", "STOP", "ok"]
for w in words:
    if w == "":
        continue
    if w == "STOP":
        break
    print(w)
print("end")

continue skips the rest of this pass. break abandons the whole loop. Watch where each one lands.

Practice break & continue

Exercise 1

Loop n over range(1, 50). Skip multiples of 3 with continue, break once n is greater than 10, and print the rest, one number per line. Expected: 1 2 4 5 7 8 10.

Exercise 2

Process the readings list in the editor below in order. A negative reading is noise: skip it with continue. A reading of 99 is the shutdown marker: stop with break. Collect earlier valid readings in accepted; keep zero.

Exercise 3

Load the parcels list in the editor below in order with limit = 10. Skip zero or negative weights using continue. Stop at the first positive parcel that would exceed the limit using break; do not try later parcels. Store accepted weights in loaded and their sum in weight. A parcel that exactly fills the limit is allowed.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest