Python Tutorial Python Advanced Python References Python Libraries

Python continue Keyword



The Python continue keyword (statement) is used to let the program skip a block of codes for current iteration in a loop. If continue statement condition is fulfilled, it brings the program to the start of loop. If the continue statement is used in a nested loop (loop inside loop), it will skip innermost loop's code block, whenever condition is fulfilled.

Continue statement with While loop:

In the example below, continue statement is used to skip the while loop if the value of variable j becomes 4.

i = 6
j = 0
while (j < i):
  j = j + 1
  if(j == 4):
    print('this iteration is skipped.')
    continue
  print(j)

The output of the above code will be:

1
2
3
this iteration is skipped.
5
6

Continue statement with For loop:

In the example below, continue statement is used to skip the for loop if the value of variable x becomes 'yellow'.

color = ['red', 'blue', 'green', 'yellow', 'black', 'white']
for x in color:
  if(x == 'yellow'):
    print('this iteration is skipped.')
    continue
  print(x)

The output of the above code will be:

red
blue
green
this iteration is skipped.
black
white

Continue statement with Nested loop

The continue statement skip the inner loop's block of codes whenever condition is fulfilled. In below mentioned example, program skips the inner loop for multiplier = 100.

# nested loop without continue statement   
digits = [1, 2, 3] 
multipliers = [10, 100, 1000]
print("# nested loop without continue statement")
for digit in digits:
  for multiplier in multipliers:
    print (digit * multiplier)

The output of the above code will be:

# nested loop without continue statement
10
100
1000
20
200
2000
30
300
3000

# nested loop with continue statement  
digits = [1, 2, 3] 
multipliers = [10, 100, 1000]
print("# nested loop with continue statement")
for digit in digits:
  for multiplier in multipliers:
    if (multiplier == 100):
      continue
    print (digit * multiplier)

The output of the above code will be:

# nested loop with continue statement
10
1000
20
2000
30
3000

❮ Python Keywords

5