In Python, the continue
statement is used to skip the rest of the current iteration of a loop and move on to the next one. It is most commonly used inside a for
or while
loop. When the continue
statement is encountered inside a loop, the program control moves immediately to the beginning of the next iteration, skipping any remaining code in the current iteration.
Here is an example of how you can use the continue
statement inside a for
loop:
# Print the numbers from 1 to 10, but skip the number 5
for i in range(1, 11):
if i == 5:
continue
print(i)
In this example, the for
loop iterates over the numbers from 1 to 10, and the if
statement checks if the current number is 5. If it is, the continue
statement is executed and the rest of the current iteration is skipped.
You can also use the continue
statement inside a while
loop in a similar way:
# Print the numbers from 1 to 10, but skip the number 5
i = 1
while i <= 10:
if i == 5:
i += 1
continue
print(i)
i += 1
The continue
statement is often used in conjunction with the break
statement, which exits a loop prematurely.
For more information on the continue
statement in Python, you
0 댓글