0

I want to understand this: var x=0 while (x<6) { x ++; } document. write (x); output was 6

12th Jun 2018, 1:25 PM
Favor Mofadeke Salako
11 Réponses
+ 6
it says: "as long as x<6, increment x". so when x=5, the while condition is still true, so x is incremented to 6. then, as x=6 the while condition becomes false and the loop stops. after the loop, document.write(x) prints the value of x, which is 6.
12th Jun 2018, 1:30 PM
ifl
ifl - avatar
+ 3
The code provided by you, will work in this manner, the loop will start from 0 and each time it will be increment because the statement given is true. When the loop will reach 5 < 6. It will be incremented for a last time making its value 6. 6 can't be greater than 6 therefore the statement will become false and it will stop the loop. The value of x will be 6.
12th Jun 2018, 1:52 PM
Akash Pal
Akash Pal - avatar
+ 2
ifl Thank you
12th Jun 2018, 1:41 PM
Favor Mofadeke Salako
+ 1
ooooh... thank you "plenty"
12th Jun 2018, 1:32 PM
Favor Mofadeke Salako
+ 1
i was actually thinking it was going to write for all the values <6 so what code would show that just to differientiate
12th Jun 2018, 1:35 PM
Favor Mofadeke Salako
+ 1
to print all values up to 6 (excluding 6): while (x<6){ document.write(x); x++; }
12th Jun 2018, 1:38 PM
ifl
ifl - avatar
+ 1
Donna #winks for you
12th Jun 2018, 1:45 PM
Favor Mofadeke Salako
+ 1
Akash Pal Thanks
12th Jun 2018, 2:24 PM
Favor Mofadeke Salako
0
Donna that means for all the conditions that are true only the last to the false will be printed right
12th Jun 2018, 1:38 PM
Favor Mofadeke Salako
0
thank you
25th Oct 2020, 8:32 PM
Israr Ud Din
Israr Ud Din - avatar
0
What’s the output of this code? var x = 0; while(x<6) { x++; } document.write(x); output is 6
4th Sep 2022, 8:49 PM
Nkah Chambeline Dzengong
Nkah Chambeline Dzengong - avatar