Python break, continue and pass statementsLet's continue with the continue statement.. continue statement The continue statement is used to skip the rest of the code inside a loop for th
Trang 1Python break, continue and pass statements
Let's continue with the continue statement
continue statement
The continue statement is used to skip the rest of the code inside a loop for the current iteration only
When the cursor encounters with a continue statement in the loop, the loop does not terminate, in this time, the loop continues on with the next iteration
Here is the syntax of continue statement:
continue
Let’s look at an example that uses the continue statement in a for loop
The use of 'continue' statements with "for" loops
In [1]: my_list = list(range(11))
for i in my_list:
if (i==4 or i==7):
continue print("The value of i:",i) The value of i: 0
The value of i: 1
The value of i: 2
The value of i: 3
The value of i: 5
The value of i: 6
The value of i: 8
The value of i: 9
The value of i: 10
The Complete Python 3 Programming Course: (Beginner to Advanced)
Trang 2In [2]: #False Usage
a = 0
while (a<10): #Do not run this code, it causes an infinite loop!
if (a==3):
continue print(a) a+=1
In [3]: #True Usage
a = 0
while (a<10):
if (a==3):
a+=1 continue print("The value of a:",a) a+=1
This is the end of continue statement
See you in our next lectures
In [ ]:
0
1
2
-KeyboardInterrupt Traceback (most recent call last)
<ipython-input-2-c0f8224399a8> in <module>()
4
5 if (a==3):
> 6 continue
7 print(a)
8 a+=1
KeyboardInterrupt:
The value of a: 0
The value of a: 1
The value of a: 2
The value of a: 4
The value of a: 5
The value of a: 6
The value of a: 7
The value of a: 8
The value of a: 9
The use of 'continue' statements with "while" loops
Don't run this code! It might have some problems on Jupyter
The Complete Python 3 Programming Course: (Beginner to Advanced)