1The continue statement in Python returns the control to the beginning of the
2while loop. The continue statement rejects all the remaining statements
3in the current iteration of the loop and moves the control back to the top of
4the loop.
5
6The continue statement can be used in both while and for loops.
1import numpy as np
2values=np.arange(0,10)
3for value in values:
4 if value==3:
5 continue
6 elif value==8:
7 print('Eight value')
8 elif value==9:
9 break
1## When the program execution reaches a continue statement,
2## the program execution immediately jumps back to the start
3## of the loop.
4while True:
5 print('Who are you?')
6 name = input()
7 if name != 'Joe':
8 continue
9 print('Hello, Joe. What is the password? (It is a fish.)')
10 password = input()
11 if password == 'swordfish':
12 break
13print('Access granted.')
1>>> for num in range(2, 10):
2... if num % 2 == 0:
3... print("Found an even number", num)
4... continue
5... print("Found a number", num)
6Found an even number 2
7Found a number 3
8Found an even number 4
9Found a number 5
10Found an even number 6
11Found a number 7
12Found an even number 8
13Found a number 9
14
1nums = [7,3,-1,8,-9]
2positive_nums = []
3
4for num in nums:
5 if num < 0: #skips to the next iteration
6 continue
7 positive_nums.append(num)
8
9print(positive_nums) # 7,3,8