Python provides the continue
statement in order to skip steps or iterations in loops. The continue statement does not end the loop, only jumps to the next step or iteration. The continue statement is generally used inside the for and while loops. Also, the if..else statements are generally used to check specific conditions to run the continue statement.
continue Statement Syntax
the continue statement has very simple syntax where it does not requires any parameter or option. It is placed inside a for or while loop for a specific condition to be met and skip the current step.
continue
Continue For Loop
the most popular usage for the continue statement is using in a for
loop. The continue statement iterates a single step further in the for loop. The for loop does not end. The for loop body is executed until the continued statement.
names = ["ismail","ahmet","ali"]
for name in names:
if name == "ahmet":
continue
print(name)
ismail ali
From the output, we can see that the “ismail” and “ali” is printed but “ahmet” is not printed as the continue statement skips print(name)
line.
Continue While Loop
The continue statement can be also used with the while
loop. In the following example, we skip the next step in a while loop if the name is equal to the “ahmet”. As a result, the “ismail” , “ali” is printed but “ahmet” is not printed.
names = ["ismail","ahmet","ali"]
i = 0
while True:
name = names[i++]
if name == "ahmet":
continue
print(name)
ismail ali