python while loop | Sololearn: Learn to code for FREE!

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

3/29/2021 11:47:16 AM

Gorden Yong Kung Hee

3 Answers

New Answer

+3

Visualize your code execution (Python, Java, C, C++, JavaScript, Ruby) https://pythontutor.com

+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"""

+1

Hope this helps you