#use of while#To be aware of the loops, consider the end of the loop.#There are 2 ways to consider the end of the loop:#1. Consider changing the condition of while in the loop body#2. Jump out of loop through break statement in the loop body#application of Method 1, the condition in which the loop body changes whilei =0 whileI < 5 : Print("1.hello World") I+ = 1#Method 2: The loop body jumps out of the loop through the break statementi =0 whileTrue:Print("2.hello World") I+ = 1ifI >= 5 : Break#while else used,i =0 whileI! = 5: Print("3.hello World") I+ = 1#mostly here, if it's not plus 1, it's going to be a dead loop.Else: Print("Print Complete")#request 1. 10 of andsum =0;i= 1; whileI <=10: Sum+=I i+ = 1Else: Print("the calculation is complete and is:", sum)#Use for Loops#used primarily in traversing collections, such as traversing strings, lists, etc.Notice="Hello, China"#Name can be any valid variable name forNameinchNotice:Print(name)#The For loop can also be used with else#For as long as the smooth execution, will go to execute else,break except, while also pay attention to this problemPets = ["Little Black","Xiao Huang","Small white"] forPetinchPets:Print(PET)Else: Print("For Loop complete")#using A For loop#Range Function forNinchRange (1,101): if(n% 2 = =0):Print(n)#Break jumps out of the loop body and ends the entire loop.#continue end this loop and continue with the next loop#Pass represents a statement, other programs do not have this implementation forNuminchRange (1,11): ifnum = = 6: Break Print(num) forNuminchRange (1,11): ifnum = = 6: Continue Print(num)
Python Loop statement (while, for)