Varun Varun - 2 months ago 8
HTML Question

JavaScript While Loop Error

<!DOCTYPE HTML>
<html>
<head>
<title>
...........
While Loops
...........
</title>
</head>
<body>
<script>
var a = 1;
while(a < 10) {
document.write("Hello World<br />")
};
a++;
</script>
</body>
</html>


Here is my while loop code when I run my code in Firefox browser or other browser like Chrome in Firefox it crashes and in Chrome it only loading occurs Right now I am following tutorial of bucky roberts from site:http://thenewboston.org/watch.php?cat=10&number=20
I have Entered the Same code which he has written in his video but then to its not working ...please some one help to find problems regarding While Loop please .. Thanks In Advance.... I have set my Variable value as
a = 1
And setting it as
a<10
and I am Incrementing it as
a++
but it is looping infinite times ..

xlm xlm
Answer

Your loop never terminates because a++ is outside of your while loop. Hence it never terminates as expected. If you look at his code again, the post incrementing of a is in the loop:

var a = 1;
while(a<10){
   document.write("Hello World<br />")
   a++;
};

Compare this with his code and yours and you'll see the difference.