Why did this code output 60 and not 50? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why did this code output 60 and not 50?

var x =10 x*= 2 while (x<30) { x+= 5 y=x } alert(y+x); I want to know in details if possible

19th Nov 2019, 3:00 PM
Abdiaziz Ahmed
Abdiaziz Ahmed - avatar
3 Answers
+ 4
20<30 is true so now inside while x=x+5 so x becomes 25 and y is also 25 since y=x. Now 25<30 is again true so now inside while x=x+5 so x becomes 30 and y is also 30 since y=x. Now 30<30 condition fails and the loop is terminated. Now both x and y are 30 so x+y=60.
19th Nov 2019, 3:04 PM
Avinesh
Avinesh - avatar
+ 2
So the loop doesn't stop unless the certain condition is achieved. Now I know where I did wrong, I looped once which gave the output 50. Avinesh thanks.
19th Nov 2019, 3:09 PM
Abdiaziz Ahmed
Abdiaziz Ahmed - avatar
+ 1
Because the loop broke when x = 30 and when y = 30
19th Nov 2019, 3:05 PM
Seb TheS
Seb TheS - avatar