VBScript loops
Imagine you have to write a program which performs a repetitive task such as counting from 1 to 100. Coding 100 lines to do this task would be mundane. There has to be an easier way, right? This is where loops come into the picture. Loops are specifically designed to perform repetitive tasks with one set of code. Loops save alot of time.
This tutorial focuses on:
- The for next loop
- The do while loop
The for next loop
The for next loop is used to repeat a task a specific number of times.
In the above example, the for next loop specifies that a loop should loop 10 times and increment the looping variable by 2 each time. Within the loop body the statement document.write(a & " ") will print the current value of the looping variable through each iteration together with a single space.
The do while loop
The do while loop works differently then the for loop. The for loop repeats a segment of code a specific number of times, while the do while loop repeats a segment of code an unknown number of times.
In the above example, a variable named num is given the value 0. The condition in the while loop is that while num is less than 30, 5 should be added to num. Once the value of num is greater than 30, the loop will stop executing. The loop prints the current value of num followed by a line break through each iteration.