Lesson 13 of 21 · 2 min
continue — Skip One Iteration, Continue the Loop
The WHY — Why continue?
Sometimes you want to skip one particular item in a loop but continue processing the rest. continue skips the rest of the current iteration and jumps to the next one.
scores = [85, -5, 92, -1, 78, 65, -3, 88] # scores with some invalid negatives
for score in scores: # go through each score
if score < 0: # check: is this score invalid (negative)?
continue # skip this negative score, go to next one
print(f"Valid score: {score}") # only reaches here for valid (non-negative) scores
Output: Valid score: 85 Valid score: 92 Valid score: 78 Valid score: 65 Valid score: 88
