![]() Capt. Horatio T.P. Webb | 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.
For-Next The primary structure for loop control is the for-next statement. The general form is:
The loop begins by:
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 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:
The loop begins by:
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++) 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:
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:
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:
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:
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:
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:
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:
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:
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:
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
|