VBScript While Loop
In VBScript (and virtually all other languages), "loops" enable your program to continuously execute a block of code for a given number of times, or while a given condition is true.
The VBScript While Loop executes code while a condition is true.
Example While Loop
This example displays the value of a counter while the count is less than or equal to 8.
This results in:
Hours worked: 1
Hours worked: 2
Hours worked: 3
Hours worked: 4
Hours worked: 5
Hours worked: 6
Hours worked: 7
Hours worked: 8
Exlanation of code
- We started by declaring a variable called "hoursWorked" and setting it to 0
- We then opened a while loop and inserted our condition. Our condition checks if the current value of the hoursWorked variable is less than or equal to 8.
- This is followed by code to execute while the condition is true. In this case, we are simply, outputting the current value of hoursWorked, preceded by some text.
- We then increment the value by 1.
- When the browser reaches the closing curly brace, if the condition is still true, it goes back to the first curly brace and executes the code again. By now, the hoursWorked variable has been incremented by 1. If the condition is not true (i.e. the variable is greater than 10), it exits from the loop, and continues on with the rest of the code.