While Loop |
|
|
While Loop The VBScript While Loop executes code while a condition is true. Example Dim numb numb = 0 While numb <= 8 document.write("Number Is: " & numb & vbCRLF ) numb = numb + 1 Wend document.write("End of While!") This results is: Number Is: 0 Number Is: 1 Number Is: 2 Number Is: 3 Number Is: 4 Number Is: 5 Number Is: 6 Number Is: 7 Number Is: 8 End of While! - We started by declaring a variable called "numb" and setting it to 0
- We then opened a while loop and inserted our condition. Our condition checks if the current value of the numb 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 numb, 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 numb variable has been incremented by 1. If the condition is not true (i.e. the variable is greater than 8), it exits from the loop, and continues on with the rest of the code.
|
|
|