HoloGuides :: Know Everything ! Knowledge
Programming
JavaScript . while



  loop : do while
  do {...} while() 
  Netscape Reference
Overture Search the Web.
  Description

do while looping is used to make sure the statements within the loop are executed at least once, since the first check is done after the first loop is already executed.
In contrary to the while combination where the first check is done before the first loop is executed. 

  Syntax

do
 {
 statements;
// this is executed at least once, then repeatedly if condition evaluates to true
 }

while(condition)

  Syntax example

myVariable=0; // make sure you start with an initialized myVariable

do 
{
statements;
// this is executed at least once and as long as myVariable is less than 10
myVariable = myVariable + 2; // myVariable should be incremented here to avoid endless loops
}
while (myVariable < 10) // use the proper comparision operator (see below)

comparison operators
== is equal
!= is different
< is less than
<= is less than or equal
>= is equal or greater than
> is greater than

 

  Working sample

 

execute some statements at least once 

lastNumber = 1;
myCounter = 1; // important : initialize your variables
  // important : use the == operator to compare, not the = to assign
do
{
myCounter = myCounter++; // this increments the myCounter variable by 1 (shortcut for myC = myC + 1)
alert(myCounter);
}
while
(myCounter < lastNumber') //even myCounter is never less than lastNumber, the statements are executed once



Banner Barter