Capt. Horatio T.P. Webb
Script Loops
VBScript and Javascript
Parks -- Fall 2001
Last Updated 5/29/2009

For array declarations see this section of the declarations page.

For array operations (functions, methods and properties) see this section of the sequentials page.

VBSCRIPT
JAVASCRIPT
 
For-Next

The primary structure for loop control is the for-next statement. The general form is:

for <index> = <start-value> to <stop-value> step <index-increment>
   .
   .
   code to be executed
   .
   .
next

The loop begins by:

  1. assigning the index the value of the start-value
  2. The value of the is tested to make sure the index is less than or equal to the stop-value
    • if true, then all statements within the loop range are processed
      (i.e., all statements down to the next statement).
      the index is then incremented by the value of index-increment,
      then the loop begins again at step 2.
    • if false, then the loop is exited and the statement following the next is executed.

The default step-increment is 1 (i.e., if you don't use step, the loop assumes 1.

To exit the loop while in process, you may use the Exit For statement to break out of ther loop at any time. Using the Exit For causes the statement following the next to be executed.

For example:

for i = 1 to 10 step 2
.
.
body of loop
.
.
next

This loop is executed for i = 1, 3, 5, 7 and 9 (i.e., five times).

 
For Loop

The primary structure for loop control is the for loop statement. The general form is:

for ( <index> = <start-value> ; <stop-test-condition> ; <index-increment>)
{

   .
   .
   code to be executed
   .
   .
}

The loop begins by:

  1. assigning the index the value of the start-value
  2. The value of the is tested in the stop-test condition
    • if true, then all statements within the loop range are processed
      (i.e., all statements between the { and }.
      the index is then incremented by the value of index-increment statement
      then the loop begins again at step 2.
    • if false, then the loop is exited and the statement following the last } is executed.

To exit the loop while in process, you may use the break statement to break out of ther loop at any time. Using the continue causes the loop to skip tto the end of the current iteration (i.e., the end of the loop) and then start at the top of the loop for the next value of the loop index.

For example:

for (i = 1 ; i<10 ; i++)
{
.
body of loop
.
.
}

This loop is executed for i= 1, 2, 3, 4, 5, 6, 7, 8, and 9 (i.e., nine times).  

Do-While (top tester true)

The general form of the Do-While is:

Do While <condition>
   .
   .
   statements to execute when the condition is true.
   .
   .
Loop

The condition is tested before the loop is begun (a top-tester).

 

Exit Do

Causes control to be transfered to the statement following the LOOP statement

 No exact Javascript equivalent (See Javascript While loop below),
Do-While (bottom tester true)

The general form of the Do While is:

Do
   .
   .
   statements to execute when the condition is true.
   .
   .
Loop While <condition>

The condition is tested only after the loop is completed (a bottom-tester). Note that the loop is always executed the first time.

Exit Do

Causes control to be transfered to the statement following the WHILE statement

Do-While (bottom tester true)

The general form of the Do While is:

Do
{
   .
   statements to execute when the condition is true.
   .
}
While <condition>

The condition is tested only after the loop is completed (a bottom-tester). Note that the loop is always executed the first time.

Do Until-Loop (top tester false)

The general form of the Do Until-Loop is:

Do Until condition
   .
   .
   statements to execute when the condition is false.
   .
   .
Loop

The condition is tested at the top and is executed only so long as the condition is false.

Exit Do

Causes control to be transfered to the statement following the LOOP statement

 

 No exact Javascript equivalent.
Do-Loop Until (bottom tester false)

The general form of the Do Loop-Until is:

Do
   .
   .
   statements to execute when the condition is false.
   .
   .
Loop Until condition

The condition is tested at the bottom and is executed only so long as the condition is false. Note that the loop is always executed once.

Exit Do

Causes control to be transfered to the statement following the LOOP statement

 No exact Javascript equivalent.
While-Wend (top tester true)

The general form of the While-Wend is:

While condition
   .
   .
   statements to execute when the condition is true.
   .
   .
wend

The condition is tested at the top and is executed only so long as the condition is true. Note that this is a variation of the Do While-Loop.

 

 
While Loop (top tester true)

The general form of the Whileloop is:

While ( condition)
{
.
.
   statements to execute when the condition is true.
.
.
}

The condition is tested at the top and is executed only so long as the condition is true.

 

For-Each

This loop is used to iterate through all the elements of an array or collection without knowing the exact number of elements in the array or collection. The general form of the For-Each is:

For Each element-type in array or collection
   .
   .
   statements to execute for each element
   .
   .
Next

Here is the vbscript code for the button below:

sub e1v
  dim famous(11)
  famous(0)="J. Pinkerton Snoopington"
  famous(1)="Mahatma Kane Jeeves"
  famous(2)="Otis Cribblecoblis"
  famous(3)="Carl La Fong"
  famous(4)="William Claude Dunkenfield"
  famous(5)="Cuthbert J. Twillie"
  famous(7)="Egbert Souse"
  famous(8)="Countess Maggie Tubbs DePuizzi"
  famous(9)="Og Oggilby"
  famous(10)="Larson E. Whipsnade"
  os=""
  for each x in famous
     os=os+"<br>"+x
  next
  e1v_out.innerHTML=os
end sub

this is the DIV with id="e1v_out"

Note that the seventh element in the array famous (i.e., famous(6) ) has NOT been defined. The code still works. Had we used a fixed increment For loop this would have generated an error. In this case the seventh item returns nothing -- but doesn't generate an error.  

For-In

This loop is used to iterate through all the elements of an array or collection without knowing the exact number of elements in the array or collection. The general form of the For-In is:

for (variable in array or collection or object)
{
   .
   .
   statements to execute for each element
   .
   .
}

  Here is the javascript code for the button below:

function e1j()
{
  var famous = new Array(11);
  famous[0]="J. Pinkerton Snoopington";
  famous[1]="Mahatma Kane Jeeves";
  famous[2]="Otis Cribblecoblis";
  famous[3]="Carl La Fong";
  famous[4]="William Claude Dunkenfield";
  famous[5]="Cuthbert J. Twillie";
  famous[7]="Egbert Souse";
  famous[8]="Countess Maggie Tubbs DePuizzi";
  famous[9]="Og Oggilby";
  famous[10]="Larson E. Whipsnade";
  os="";
  for (x in famous)
    {
     os=os+"<br>"+famous[x];
    }
  e1j_out.innerHTML=os
}

this is the DIV with id="e1j_out"

Note that the seventh element in the array famous (i.e., famous[6] ) has NOT been defined. The code still works. Had we used a fixed increment For loop this would have generated an error. Compare to vbscript version to the left, we must use the array name inside the For-In.

There is a For-Each in javascript -- but it is NOT recommended for arrays -- only for objects.

Exit For

Causes control to be transfered to the statement following the Next statement