Disclaimer: This article is based on the analysis of [multiple articles] code by Xiaobai. If there is any error, thank you for pointing it out!
^ ^ (reliable)
The analysis process below is extremely detailed, but it is relatively long. If you cannot understand it, you can refer to it.
1. Answer
Both break and continue are for while or for loops, no matter how many ifs (or none), it has nothing to do with it.
The following is understood with pictures and code templates.
1. Picture
(regardless of if, nothing to do with it)
2. Code template (take while as an example)
3. Understand
break is equivalent to making the condition of the while loop not true, thus exiting the while
continue is equivalent to commenting out the code below it in while, thus looping again
Note: If you need to use def and while at the same time, while must be included:
correct:
def a():
while True:
return 1/1
break
print(a())
mistake:
while True:
def a():
return 1/1
break
print(a())
Second, the analysis process (take while as an example)
When one piece of code is flawed, the next piece can fix it.
1、break
(1)
a=1
while a<10:
a+=1
if a==4:
break
print(a)
print(a)
result:
2
3
4
analyze:
The third row: a=2
Line 6: print a=2
return the second row
The third row: a=3
Line 6: print a=3
return the second row
The third row: a=4
The fourth line: enter if
The fifth line: jump out of the current while and enter the seventh line
Line 7: print a=4
Defect: I don't know if it is to jump out of all while
(2)
a=1
while a!=0:
a+=1
while a<10:
a+=1
if a==4:
break
print(a)
print('……')
break
print(a)
result:
3
……
4
analyze:
The third row: a=2
Fifth row: a=3
Eighth line: print a=3
return the fourth row
Fifth row: a=4
Line 6: enter if
Line 7: Jump out of while(2) and enter line 9
Line 9: prints...
Line 10: Jump out of while(1) and enter line 10
Line 11: print a=4
Defect: I don't know how if will affect it
(3)
a=1
while a==1:
print('^_^')
break
print('T_T')
print(': )')
result:
^_^
: )
analyze:
The third line: print ^_^
The fourth line: jump out of the current while and go to the sixth line
Line 6: Print : )
Defect: I don't know how multiple ifs affect it
(4)
a=1
while a!=0:
a+=1
while a<10:
a+=1
if a==4 or a==5:
if a==5:
break
print(': P')
print(a)
print('……')
break
print(a)
result:
3
: P
4
……
5
analyze:
The third row: a=2
Fifth row: a=3
Line ten: print a=3
return the fourth row
Fifth row: a=4
Line 6: enter if(1)
Line 9: print: P
Line ten: print a=4
return the fourth row
Fifth row: a=5
Line 6: enter if(1)
Line 7: enter if(2)
Line 8: Jump out of while(2) and enter line 11
Line 11: prints...
Line 12: Jump out of while(1) and enter line 13
Line 11: print a=5
get the answer
2、continue
This section compares with break, only replaces all breaks with continue
(1)
a=1
while a<10:
a+=1
if a==4:
continue
print(a)
print(a)
result:
2
3
5
6
7
8
9
10
10
analyze:
The third row: a=2
Line 6: print a=2
return the second row
The third row: a=3
Line 6: print a=3
return the second row
The third row: a=4
The fourth line: enter if
The fifth line: go back to the beginning of while and enter the second line
The third row: a=5
Line 6: print a=5
return the second row
Repeat the above bold part, print a=6, a=7, a=8, a=9 respectively
The third row: a=10
Line 6: print a=10
Return to the second line, because a<10 is not true, exit the loop and enter the seventh line
Line 7: print a=10
Defect: I don't know if it is to jump out of all while
(2)
a=1
while a!=0:
a+=1
while a<10:
a+=1
if a==4:
continue
print(a)
print('……')
continue
print(a)
result:
3
5
6
7
8
9
10
……
……
……
(无限循环)
analyze:
The third row: a=2
Fifth row: a=3
Eighth line: print a=3
return the fourth row
Fifth row: a=4
Line 6: enter if
Line 7: Go back to the beginning of while (2) and enter line 4
Fifth row: a=5
Eighth line: print a=5
return the fourth row
Repeat the above bold part, print a=6, a=7, a=8, a=9 respectively
Fifth row: a=10
Eighth line: print a=10
Return to the fourth line, because a<10 is not true, exit the while loop and enter the ninth line
Line 9: prints...
Line 10: Go back to the beginning of while(1) and enter the second line
The third line: a assignment (it doesn't matter how much a is equal to, as long as a<10 is not true)
Line 9: prints...
Line 10: Go back to the beginning of while(1) and enter the second line
Repeating the bolded part above, resulting in an infinite loop (printing...)
Defect: I don't know how if will affect it
(3)
a=1
while a==1:
print('^_^')
continue
print('T_T')
print(': )')
result:
^_^
^_^
^_^
(无限循环)
analyze:
The third line: print ^_^
The fourth line: go back to the beginning of while and go to the second line
The third line: print ^_^
Repeat the bold part above, resulting in an infinite loop (print ^_^)
Defect: I don't know how multiple ifs affect it
(4)
a=1
while a!=0:
a+=1
while a<10:
a+=1
if a==4 or a==5:
if a==5:
continue
print(': P')
print(a)
print('……')
continue
print(a)
result:
3
: P
4
6
7
8
9
10
……
……
……
(无限循环)
analyze:
The third row: a=2
Fifth row: a=3
Line ten: print a=3
return the fourth row
Fifth row: a=4
Line 6: enter if(1)
Line 9: print: P
Line ten: print a=4
return the fourth row
Fifth row: a=5
Line 6: enter if(1)
Line 7: enter if(2)
Line 8: Go back to the beginning of while (2) and enter the fourth line
Fifth row: a=6
Line ten: print a=6
Go back to the beginning of while (2) and enter the fourth line
Repeat the above bold part, print a=7, a=8, a=9 respectively
Fifth row: a=10
Line ten: print a=10
Return to the fourth line, because a<10 is not true, exit the while loop and enter the 11th line
Line 11: prints...
Line 12: Go back to the beginning of while (1) and enter the second line
The third line: a assignment (it doesn't matter how much a is equal to, as long as a<10 is not true)
Line 11: prints...
Line 12: Go back to the beginning of while (1) and enter the second line
Repeating the bolded part above, resulting in an infinite loop (printing...)
get the answer
end
Disclaimer: This article is based on the analysis of [multiple articles] code by Xiaobai. If there is any error, thank you for pointing it out!
^ ^ (reliable)
=================================== The answer is at the top =============== ==================