+ 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 />");
29 odpowiedzi
+ 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.
+ 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.
+ 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.
+ 4
you should look at the "{}" where is beginning and ending
+ 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.
+ 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
+ 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?
+ 2
because in case 1 the result is displayed first and incremented after. where in case 2 it's just opposite
+ 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
+ 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.
+ 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.
+ 1
Go line by line and dry run the code.. you will find answer yourself
+ 1
because while work while i<10 but if you put it down out of while it will work just once
+ 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
+ 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
+ 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
+ 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
+ 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!
+ 1
so obvious from the code. just document.write is inside the loop
+ 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.:)