How to use continue and break statements in Python
Today, I will talk to you about how to use continue and break statements in Python. Many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
1. Continue
First look at continue,Enter loop, the loop starts, and then the test condition of the loop, if it is false, then directly jump out of the loop; if it is true, go to continue, determine whether the continue is true or false, if it is true, the loop returns to the test condition at the beginning, jump out of the current loop step, continue to the next loop, if false, the loop continues to execute the remaining statements.
2.break statement
Enter loop, the test condition under which the loop starts and the loop starts. If it is false, the loop ends; if it is true, it goes to the break statement to determine whether the break statement is true or false; if it is true, the loop ends. If false, continue to execute the statement below the loop.
If you don't understand the picture, it doesn't matter. Take a look at some chestnuts and you'll understand. First look at the code, and then you can guess the result first, and finally look at the following output.
For i in range (10):
If I = = 4 or I = 7:
Continue
Print i
Print "- * -" * 10
For i in range (10):
If I = = 4 or I = 7:
Break
Print i
Print "- * -" * 10
For val in "machine learning":
If val = "I":
Continue
Print val
Print "- * -" * 10
For val in "machine learning":
If val = "I":
Break
Print val
Print "- * -" * 10
Val1 = 10
While val1 > 0:
Val1-= 1
If val1 = = 4 or val1 = = 7:
Continue
Print val1
Print "- * -" * 10
Val2 = 10
While val2 > 0:
Val2-= 1
If val2 = = 4 or val2 = = 7:
Break
Print val2
"
0
one
two
three
five
six
eight
nine
-*
0
one
two
three
-*
M
A
C
H
N
E
L
E
A
R
N
N
G
-*
M
A
C
H
-*
nine
eight
six
five
three
two
one
0
-*
nine
eight
"
After reading the above, do you have any further understanding of how to use continue and break statements in Python? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.