In Python, we can change the flow of a loop using the control statements:
continue
break
pass
The continue
statement stops the current iteration and goes to the next.
for n in range(0, 5):if n == 2:continueprint(n)
The break
statement breaks out of the innermost enclosing for
or while
loop.
for n in range(0, 5):if n == 2:breakprint(n)
The pass
statement does nothing, and is essentially a null statement. For example, it can be used in an if
statement to skip the current iteration. It can also be used as a placeholder for future code.
for n in range(0, 5):if n == 2:passelse:print(n)