- 7

# What's the output of this code? i = 0 x = 0 while i < 4: x+=i i+=1 print(x)

6 Answers

+ 3

Why don't you try running in playground?!

+ 2

YOKENTHIRAN THANUJA ,
please use playground to present your code in future. people don't like to copy, paste and rework your code if they are asked to help you.
thanks for your understanding!

0

Correct your indentation also

0

i=0
x=0
0<4
x=0+0
i=0+1
i=1
x=0
1<4
x=0+1
i=1+1
i=2
x=1
2<4
x=2+1
i=2+1
i=3
x=3
3<4
x=3+3
i=3+1
i=4
x=6
4<4
loop ends and print the value of x which is 6

- 1

could someone please explain how the answer is 6, i ran the same code and it is an infinite loop, so i am confused. thank you

- 2

i don't understand the logic, why the answer is 6? looking for explanation
i = 0
x = 0
while i < 4:
x+=i
i+=1
print(x)