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

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

30th Apr 2021, 7:35 AM
Jagadeesh Latha
Jagadeesh Latha - avatar
10 Answers
+ 11
0 i = 0 x = 0 while i < 4: x+=i i+=1 print(x) #it says that after loop end value is print loop start at i = 0 and x will be x = x+i = 0 + 0=0 Iteration2 in the loop i value as 1 by increment (i+=1) and x will be x = x+i = 0+1 = 1 Iteration 3 in the loop i value as 2 by increment and x will be x = x+i = 1 + 2 = 3 Iteration 4 in the loop i value as 3 by increment and x will be x = x+i = 3 +3 = 6 Iteration 5 in the loop i value as 4 by increment and the condition i < 4 is false so it fail to run the loop and the answer will take from the last iteration of the loop and the last iteration x is equal to 6
16th Sep 2021, 1:16 PM
Aarun Kutty
Aarun Kutty - avatar
+ 4
x = x + i i=i+1 #intial x=0 and i=0 0= 0 + 0=0 1 = 1 + 1 =2 2 = 2 + 2 =4 3 = 3 + 3 =6
10th Oct 2021, 7:53 AM
Pratap Vasava
Pratap Vasava - avatar
+ 3
everyone's saying its 6. But how?
26th Aug 2021, 5:49 PM
chapchap2
chapchap2 - avatar
+ 1
So I understand why it's 6 even though the max of x is 4. Just because the max for x is 4 doesn't mean that the output can't be higher than 4. But that's about all I understand.
17th May 2022, 8:51 PM
My Finance
0
please help me
3rd May 2021, 3:45 PM
YOKENTHIRAN THANUJA
YOKENTHIRAN THANUJA - avatar
0
i = 0 x = 0 while i < 4: x+=i i+=1 print(x) Réponse 6
4th Aug 2021, 6:33 PM
MBOUBA SOUBEYE Denis
MBOUBA SOUBEYE Denis - avatar
0
Answer=6
24th Aug 2021, 7:20 PM
tiwa adebisi
tiwa adebisi - avatar
0
6
27th Aug 2021, 1:48 PM
PRINCE KUMAR
PRINCE KUMAR - avatar
0
But how the 6 is coming pls explain
8th Sep 2021, 3:37 PM
AMITABH DASGUPTA B4
AMITABH DASGUPTA B4 - avatar
- 1
Ans: 6
18th May 2021, 9:32 AM
Md. Zahidul Islam
Md. Zahidul Islam - avatar