0

python while loop

I have difficulties understanding how this code outputs the values below. Code: i = 0 x = 0 while i < 4: print(x) x+=i i+=1 Output: 0 0 1 3

29th Mar 2021, 11:47 AM
Gorden Yong Kung Hee
Gorden Yong Kung Hee - avatar
3 Réponses
+ 4
Visualize your code execution (Python, Java, C, C++, JavaScript, Ruby) https://pythontutor.com
29th Mar 2021, 11:52 AM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 1
i = 0 x = 0 while i < 4: print(x) #x=0,x=0,x=1,x=4 x+=i #x=0 since i=0,now x=1,x=3 i+=1 #now i=1,i=2,i=3 """Output: 0 0 1 3"""
29th Mar 2021, 12:25 PM
Atul [Inactive]
0
Hope this helps you
29th Mar 2021, 12:25 PM
Atul [Inactive]