+ 1

Regarding A While Loop, Please help !

Would you mind explaning why I get 6 as an outcome? How is it possible under i<=5 ? A newbie here with a little mathematical background, please take it easy on me :). i = 1 while i <=5.0: print(i+1) i = i+2 print("Finished!") Outcome: 2 4 6 Finished!

23rd Dec 2018, 8:34 AM
M.Kemal
3 ответов
+ 7
First time when i=1 1<5 condition true Print(i+1) so print(1+1)=2 Then i= i + 2 so value of i is 1 so i =1+2=3 now i = 3 Again in loop 3<5 true Print(3+1) =4 so print 4 Value of i is 3 Now i = i + 2 = 3+ 2 =5 So 5<=5.0true Print(5+1)= 6 so print 6 Now i = i + 2 so i = 5 + 2= 7 7<=5.0 false Loop terminate and print 2 4 6 As output
23rd Dec 2018, 8:40 AM
MsJ
MsJ - avatar
+ 5
Welcome😊
23rd Dec 2018, 8:48 AM
MsJ
MsJ - avatar
+ 2
Thank you so much !
23rd Dec 2018, 8:44 AM
M.Kemal