Java Reference
In-Depth Information
do ... while Loops
A do ... while loop is similar to a while loop. The only difference is that the con-
dition comes after the block of code:
do {
do something
} while(condition)
This means that the block of code will always be run at least once, regardless of the condi-
tion being true or not.
Here's the same example we saw before, rewritten as a do ... while loop:
var bottles = 10;
do {
alert("There were " + bottles + " green bottles, hanging
on the
wall. And if one green bottle should accidently fall,
there'd be
" + (bottles-1) + " green bottles hanging on the wall");
} while (bottles > 0)
for Loops
for loops are by far the most common in JavaScript and take the following form:
for (initialization ; condition ; after) { do something }
The initialization code is run before the loop starts and is usually employed to initialize any
variables used in the loop. The condition has to be satisfied for the loop to continue. The
after code is what to do after each iteration of the loop, and it is typically used to increment
a counter of some sort.
Here's the green bottles example written as a for loop:
Search WWH ::

Custom Search