+ 8

Can anyone explain the result of this while loop?

var i = 0; while(i<6){ i++; } document.write(i);

3rd Apr 2017, 6:02 PM
Marshall Moyo
Marshall Moyo - avatar
20 odpowiedzi
+ 32
The output is 6. The while loop runs for the values of i from 0 to 5. When it is 5, i is incremented to 6. Then the condition of the while loop is false (6<6 is false) and the loop stops. Now i is printed with it's current value which is still 6. [Edit] I'm too late, finished typing after the others ^^
3rd Apr 2017, 6:07 PM
Tashi N
Tashi N - avatar
+ 16
this loop will continue till i is < 6. First time :- i=1 2nd :- i=2 3rd :- i=3 4 th:- i=4 5th :- i=5 Here ,now i is still less than 6, hence, 6th:- i=6, Now i is = 6 but not less than 6 ,Hence output is 6
3rd Apr 2017, 6:05 PM
Meharban Singh
Meharban Singh - avatar
+ 4
it will repeat post increment on i until i equals 6 at that point the while loop stops and the value of i (6) is printed.
3rd Apr 2017, 6:08 PM
seamiki
seamiki - avatar
+ 4
Variable i is 0 and while i is smaller than 6, the looping will continue. 1. Because i = 0 and the condition 0 < 6 is true, program will execute the code inside the looping. 2.Inside the looping: i++ is similar to i = i + 1. The variable i will be incremented by 1 everytime it is looping. 3. after i=5 it won't stop because it is still smaller than 6 and the program will execute the code inside looping. so i+1 is 6. 4. Because 6<6 condition return false, the looping stop. Sorry for my bad english. :)
9th Apr 2017, 6:31 AM
Chandra Wibowo
Chandra Wibowo - avatar
+ 2
The result is 6 i=0 the program verify, 0<6, i++=1 i=1, 1<6, i++=2 i=2, 2<6, i++=3 i=3, 3<6, i++=4 i=4, 4<6, i++=5 i=5, 5<6, i++=6 i=6, 6=6, it's not less, the loop end there, and the program print 6.
7th Apr 2017, 10:30 PM
cedric ananga
cedric ananga - avatar
+ 2
on the last iteration, i is equal 5 and i will be increased by one to 6. now: 6<6 isn't true anymore, so the while loop will break. now i equal 6, and 6 will be printed.
18th Apr 2017, 1:13 PM
anon
anon - avatar
+ 1
I know there are plenty replys already but the output is 5 beacause you put i<6, it would output six if you put i<=6 I hoped this helped :D
8th Apr 2017, 7:04 PM
BabyBacon
BabyBacon - avatar
+ 1
the loop increase until 6 remember 0 is the first position and start the loop 0+1
9th Apr 2017, 1:30 PM
Erich Daniel Galvez Carranza
Erich Daniel Galvez Carranza - avatar
0
Loop continues until i = 6, next the code write i (=6).
4th Apr 2017, 10:52 AM
Vojtěch Novák
Vojtěch Novák - avatar
0
Answer will be 6!
5th Apr 2017, 3:30 PM
Jyoti Kumari
0
I=6
6th Apr 2017, 6:52 PM
aakriti saxena
0
loop goes till i=5 then get terminated
8th Apr 2017, 6:04 PM
Rohan Singh
Rohan Singh - avatar
0
hallo
9th Apr 2017, 10:15 AM
Sevda Sarac
Sevda Sarac - avatar
- 1
i think it adds up until i<6 becomes invalid
3rd Apr 2017, 7:29 PM
Archil Melkadze
Archil Melkadze - avatar
- 2
output is 1 2 3 4 5 .and there no 6 cuz the code is <6
4th Apr 2017, 7:11 PM
Benaicha Abd El Qodus
Benaicha Abd El Qodus - avatar
- 2
jyoti can i contact u pls......my email is yugalpathak2012@gmail.com
8th Apr 2017, 4:44 AM
Yugal Pathak
Yugal Pathak - avatar
- 3
The final output of this loop will be 5. Every time the loop runs, i is incremented by 1 (i++) The loop runs until i is 5 because (i<6) is true as long as i is 5 or less.
3rd Apr 2017, 6:06 PM
Hassie
Hassie - avatar
- 3
6
5th Apr 2017, 4:08 AM
Vishal Rohilla
Vishal Rohilla - avatar
- 3
You need to put <=6 if you don't want enter in a loop.
5th Apr 2017, 11:56 AM
Lisandro Arnodo
Lisandro Arnodo - avatar
- 5
6
4th Apr 2017, 1:11 PM
Shakeb Tawheed
Shakeb Tawheed - avatar