while loop | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

while loop

Why does the output for this is 6 instead of 4?? var x = 3; var a = 0; while (a < 3) { x += 1; a += 1; } println(x);

26th Dec 2016, 3:14 AM
totoro
totoro - avatar
2 Answers
+ 3
execution will be like this: pass1: a=0 0<3 true x=3+1= 4 a=0+1=1 pass2: a=1 1<3 true x=4+1= 5 a=1+1=2 pass3: a=2 2<3 true x=5+1=6 a=2+1=3 pass4: a=3 3<3 false output: x=6
26th Dec 2016, 3:31 AM
Ravi Kumar
Ravi Kumar - avatar
+ 2
@Ravi Thank you that make so much sense
26th Dec 2016, 4:37 AM
totoro
totoro - avatar