In the last issue, we understood and learned ternary operations and and;or;not in logical operations.
In this issue, we will learn about the while loop in the future.
if Judgment can choose to do something, but it can only be done once, if you have to repeat the judgment a lottime?This is where our while loop comes into play.
# Assign value a is 1a = 1# enter the loopwhile a < 5:print(a)a += 1'''The while loop cannot stop by itself, so add a += 1'''
whileUse andifclassesLike, sentencedtruewhenRunIndent the code insideCode, the judgment span is falsetimesstop looping;Usewhilespan> Attention should be paid to the loop:The loop must be able to stop;Note whether the judgment formula can beFake.
The process of the whole loop is just like the above table, and it is executed in sequence until the end of the loop.
a = 1while a < 5:print(a)a += 1else:print("End of loop")
The while loop can also be followed by an else loop. When the loop ends normally, the code in the else will be executed.
a = 1while True:if a % 5 == 0:breakprint(a)a += 1else:print("End of loop")
while can also follow True directly, but internally you must use break to terminate the loopA loop terminated with break is the content of the else that will not be executed.
We learned about the while loop in this issue