+ 2
What's the output of this code? i = 0 x = 0 while i < 4: x+=i i+=1 print(x)
11 odpowiedzi
+ 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
+ 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
+ 3
everyone's saying its 6. But how?
+ 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.
0
please help me
0
i = 0
x = 0
while i < 4:
x+=i
i+=1
print(x)
Réponse 6
0
Answer=6
0
6
0
But how the 6 is coming pls explain
- 1
Ans: 6