Why the result of these two loop is different? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Why the result of these two loop is different?

var i=1; while (i<10) { document.write(i + "<br />"); i++; } vs var i=1; while (i<10) { i++; } document.write(i + "<br />");

17th Feb 2017, 5:58 AM
Seyed Ebrahim Khadem
Seyed Ebrahim Khadem - avatar
29 Answers
+ 27
instead of giving you the answer I'm trying to help you to find it by yourself: what did you expect? maybe write down every step of the loops as if there's no loop and you do everything manually like this: first step: i is 1, is i less than 10? yes, console output 1, i becomes 2 second step: i is 2... and so on you'll see the difference quite clearly.
17th Feb 2017, 6:11 AM
Mario L.
Mario L. - avatar
+ 17
For the first one, the printing function is inside the while loop which means that it will print out every single number 'i' for each iteration until it terminates.. The result will be a long list of numbers. The second one, the printing function is outside the while loop. Now, for loop function, it always keeps looping until the condition is false, then the next line of code is run. In this case, the code inside the while loop only increments the value of 'i'. When the loop breaks, the document.write() function is executed and what is printed out is only 1 value, which is the latest and updated value of 'i'. The output will only be one line. Therefore, both of the code will give different outputs.
17th Feb 2017, 6:12 AM
Deddy Tandean
+ 5
The first loop prints each iteration of the loop with a page break between them. The second loop completes. Then prints it's last iteration once due to the print statement being outside the loop.
18th Mar 2017, 1:57 PM
Louis
+ 4
you should look at the "{}" where is beginning and ending
19th Mar 2017, 9:39 PM
Jakub Kleban
+ 3
1 : in loop 1 value of I will keep printing till it satisfy loop condition because I is inside loop. 2. but in second loop..while loop will keep running till it satisfy loop condition and after that it will print value of I. let me know if you need exact answer/clarification or anything else here.
19th Feb 2017, 8:10 AM
Raj Kumar Chauhan
Raj Kumar Chauhan - avatar
+ 2
In first loop you have written the statement document.write(i +"<br /> inside the while loop so everytime the loop run it will print whatever written in the statement for your second loop you have written the statement outside the while loop so it will only print one time
20th Feb 2017, 2:47 PM
Mohit Sahu
Mohit Sahu - avatar
+ 2
The first one is writing i, then incrementing it, then writing the next i. the second one increments i nine times then writes just the last iteration. you put the write statement outside the loop. which one were you shooting for?
21st Feb 2017, 7:22 PM
Jake Ritter
Jake Ritter - avatar
+ 2
because in case 1 the result is displayed first and incremented after. where in case 2 it's just opposite
8th Mar 2017, 10:12 PM
MAHESHWARI TRIPATHI
MAHESHWARI TRIPATHI - avatar
+ 1
Maybe my reply is quite long ,but i bet that you can easily get it Personally , in the first one , the console can read like while (i<10) it will print out the number i and then i increase ,so when i=1 (at first ) it prints out 1 time and then increase i from 1 to 2 , and continue printing the number i , then increase i from 2 to 3 , and so on But in the second one , the console understands like this , first i equals 1 , then nothing happen(because there's no command from you,dude),after that i automatically increases from 1 to 2 , and 3 to 4 and 4 to 5 and so on until i reaches 10 ( i<10 but i increase to 10 before it check the condition ) ,so the latest value of i is now 10 .Finally you print out i with the value 10 Hope this will be helpful , i'm a newbie ,too :D
20th Feb 2017, 1:44 PM
Thành Long
Thành Long - avatar
+ 1
In the first loop, you write the value of the variable I at each loop. While in the second 1 you write the value only once when all the loops are done.
22nd Feb 2017, 3:25 AM
Philippe Cotte
Philippe Cotte - avatar
+ 1
I the first loop the value of I would from 1 to 10 so document.write would be used 10 times were as in the second loop the value of I would be only 10 since it is outside the while loop so document.write would be used only once with value 10.
23rd Feb 2017, 6:58 AM
NirmallKiruthik
NirmallKiruthik - avatar
+ 1
Go line by line and dry run the code.. you will find answer yourself
26th Feb 2017, 5:30 AM
nishant sethi
nishant sethi - avatar
+ 1
because while work while i<10 but if you put it down out of while it will work just once
28th Feb 2017, 10:14 PM
Alex
Alex - avatar
+ 1
after taking a quick glance at the program, I can see that in the first loop condition, "i" will be printed to the console window only if the while statement holds true through each iteration. for the second loop, only the final or last value of "i" will print once the when the while loop evaluates to false
1st Mar 2017, 3:40 AM
Anthony Nero
Anthony Nero - avatar
+ 1
because in first Loop,the document.write statement is defined within the loop.each and every time the " i " value is printed while the loop runs .but in 2nd loop, document.write statement is defined outside the loop..once the loop terminate then then control transfer to the document.write statement..so it print the "I "value once
1st Mar 2017, 12:57 PM
SUGANTHI M
SUGANTHI M - avatar
+ 1
What value you want to write on your document, on the first, you write all of i value except the last one. But on the second you only write the last value of the i
6th Mar 2017, 10:20 PM
Arif Surahman
Arif Surahman - avatar
+ 1
in first loop you are writing write statement within loop so it will print output till loop breaks and next statement is after loop which only print output once because it is not in loop. you still need to learn basic loop concepts and flow control
10th Mar 2017, 4:18 AM
Gahan Saraiya
Gahan Saraiya - avatar
+ 1
Because the first loop contains a print function inside the braces, so it does the print in each iteration. And thats the opposite of the second loop that does the print after all iterations is done!
12th Mar 2017, 11:04 PM
majdi abu salah
+ 1
so obvious from the code. just document.write is inside the loop
14th Mar 2017, 10:27 AM
Abbosjon Kudratov
Abbosjon Kudratov - avatar
+ 1
The first program prints with a line break after each number while the second one prints all numbers in a line and then a single line break at the end.:)
15th Mar 2017, 7:27 PM
Monsij Biswal
Monsij Biswal - avatar