What's the output of this code? i = 0 x = 0 while i < 4: x+=i i+=1 print(x) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 7

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

3rd May 2021, 3:48 PM
YOKENTHIRAN THANUJA
YOKENTHIRAN THANUJA - avatar
6 Answers
+ 3
Why don't you try running in playground?!
3rd May 2021, 3:49 PM
Scarlet Witch
Scarlet Witch - avatar
+ 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!
3rd May 2021, 4:47 PM
Lothar
Lothar - avatar
0
Correct your indentation also
3rd May 2021, 4:32 PM
Atul [Inactive]
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
6th Nov 2022, 1:08 AM
Ajay Kumar
Ajay Kumar - avatar
- 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
16th Oct 2022, 9:32 PM
Alpha Habib
Alpha Habib - avatar
- 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)
2nd Sep 2021, 1:42 PM
Fidella Tee Pi-Ler
Fidella Tee Pi-Ler - avatar