0
please, won't this code give the same result. i=0 while i<=5 print(i) i+=1 print("finished") and i=0 while 1==1 print(i) i+=1 if i<=5 print ("finished")
8 Réponses
+ 2
see after the if statement in last but one line there is no break statement to come out of the loop and also you should only mention
if i==5:
break
.......
I hope you get the difference
+ 1
2nd code will run forever if you don't include a break condition
0
The bottom code:
While 1 == 1. Do you mean for the variable to be i?
0
you wont get out of the loop.in 2nd one
0
the loop would never terminate as 1 is always equal to 1 and there is no break statement for if
0
no break are used
0
you second loop says each iteration less or equal 5 print (finished) then the loop continues to infinity adding +1 each iteration
- 1
The if statement in the second code must have a break statement to go out of loop.