The Continue statement in Python
The python continue statement is used to skip the current block of for loop or while loop. It will not execute the next statements and move the pointer to the beginning of the for or while loop.
Example of using continue with while loop
The example below will not display 3, because as soon as the variable i reaches the value=3 it will encounter the continue statement, where loop skips and move to the beginning of the while loop.
i = 1
while i < 5:
i = i +1
if i == 3:
print (‘Current value :’, i)
The output will be:
Current value: 2
Current value: 4
Current value: 5
The continue with for loop example
The following examples uses Python continue with the for loop. We placed the continue statement as the variable j value is ‘c’ in the loop.
i = [‘a’,‘b’,‘c’,‘d’]
for j in i:
if j == ‘c’:
Output will be:
You can see, the loop only displayed a,b and d.
Also see – The For loop | while loop
Leave A Comment?