+ 1

Does anyone know how this code is worked out?

i = 0 while 1==1: print(i) i = i + 1 if i >= 5: print("Breaking") break print("Finished") >>> 1 2 3 4 5 breaking finished >>>

28th Apr 2017, 10:17 AM
Benstead Towara
Benstead Towara - avatar
3 Réponses
+ 10
0 1 2 3 4 Breaking Finished
22nd Oct 2017, 5:12 AM
niteOwLTwO
niteOwLTwO - avatar
+ 8
infinite loop count to 5 and break infinite loop much easier to just say while i <=5 print(i) but if looking at the break function this is an example of how to works
28th Apr 2017, 10:23 AM
jay
jay - avatar
+ 3
i = 0 #assign integer 0 to variable i while 1==1: #start loop while true print(i) #prints the variable i after iteration below i = i + 1 #substitute 0 in i you get i = 0 + 1 = 1 if i >= 5: # iteration continues until this condition print("breaking")#when condition met, print this break #the break statement ends the loop print(Finished) #outside of loop, printed after break The variable (integer) is run through the iteration, with each value being stored in memory and run again, like so: i = 0 i = i + 1 is i = 0 + 1 = 1 thus i = 1 now (stored in mem) so i = i + 1 is now i = 1 + 1 = 2 and so on, hence printing i each time run through the iteration i.e. 1 2 3 4 5 (break - because i is now greater than or equal to 5)
4th May 2017, 12:54 PM
Nathanael Pomat
Nathanael Pomat - avatar