Which statement skips the rest of the code inside a loop and starts the next iteration?
a) pass
b) break
c) continue
d) return
Answer:
c) continue
Explanation:
The continue
statement in Python is used within loops to skip the rest of the code inside the current iteration and immediately move to the next iteration of the loop. This is particularly useful when you want to bypass specific parts of the loop under certain conditions.
for i in range(5):
if i == 2:
continue # Skips the rest of the code for i == 2
print(i) # Prints 0, 1, 3, 4
In this example, when i
equals 2, the continue
statement is executed, causing the loop to skip the print()
statement for that iteration and move on to the next iteration.
The continue
statement allows you to fine-tune the behavior of loops, enabling you to handle specific cases without disrupting the overall loop execution.