Jump to content

JS while Loop Question.


shreyaskudav

Recommended Posts

<html><body><script type="text/javascript">var i=0;while (i<=5) { document.write("The number is " + i); document.write("<br />"); i++; }</script></body></html>
Why is the increment added within the function and not parentheses() ???, Instead its added in the for loop?
Link to comment
Share on other sites

Why is the increment added within the function and not parentheses() ???, Instead its added in the for loop?
i used to think that for() meant having those 3 'pieces' as the syntax of that function, but if you view it this way;
for (i=0;i<=limit;i++)

it starts to make more sense, and you can apply that to while() and see what is really going on.

Link to comment
Share on other sites

while() and for() are not functions. They behave differently. Things that are within the block are not taken out of scope like in functions.While behaves like this:

As long as the condition (i is less than or equal to 5) is true, do the following:{  print i  add 1 to i (i++)}

For behaves like this:

Run the specified block of code with the following three conditions:  1. Before starting any of the code: set i to 0  2. Keep running the code as long as: i is less than or equal to 5  3. When the loop is finished: add 1 to i (i++){  print i}

Link to comment
Share on other sites

One of my teachers put it like this. You use a for loop when you know how many times you're going to be looping. You use a while loop when you need to loop as long as some condition is true, or something is happening. You use a do loop when you need to loop until something happens (or doesn't happen).If you're looping over an array, use a for loop, because you know how many elements are in the array.

var ar = [1, 2, 3, 4, 5];for (var i = 0; i < ar.length; i++){  alert(ar[i]);}

In that example you know you need to loop ar.length times, so you use a for loop. A while loop would work when something may or may not be happening (a condition is either true or false), and you want to loop as long as it's happening. Frankly, most Javascript code doesn't have a lot of need for while loops. An example in PHP would be to keep reading data from a file until you've read it all.

<?php$handle = fopen("http://www.example.com/", "rb"); // open a remote file$contents = '';while (!feof($handle)) { // loop until the end of the file is found  $contents .= fread($handle, 8192); // add the file data to a buffer}fclose($handle); // close the file?>

You use a do loop when you want to loop at least once. A while loop will loop 0 or more times, a do loop will loop 1 or more times. Another example from PHP is creating a filename that doesn't exist yet:

$directory = 'images/';do{  $filename = md5(time() . mt_rand()); // make a random name} while (file_exists($directory . $filename)); // loop again if it already exists

Link to comment
Share on other sites

One of my teachers put it like this. You use a for loop when you know how many times you're going to be looping. You use a while loop when you need to loop as long as some condition is true, or something is happening. You use a do loop when you need to loop until something happens (or doesn't happen).......
hmm, nice & concise - so;IF
DSKwallet = are_you_kidding_me;escort_price = DSK2stingy;

for loop

for (escort_price=0; escort_price<=DSKwallet; escort_price++) {DSKhasfun();}

while loop

while (DSKwallet!==0)  {DSKhasfun();DSKwallet-=escort_price;}

do loop

do  {DSKhasfun();} while (!DSKbusted)

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...