VBScript: Functions and Loops - Loop Flakes...They're Great!
(Page 3 of 6 )
Earlier, I said that functions save lazy programmers time. Well, I have more good news for all you slackers: so do loops. Loops allow programmers to repeat blocks of code a certain amount of times, depending on the type of loop and the criteria the programmer has set upon them. There are several types of loops in VBScript.
The For...Next Loop
If you know in advance how many times you would like a particular line of code to execute, you can use the For...Next Loop. The For Loop consists of two statements: the For statement, which creates the counter variable and assigns it a start and end value, and the Next statement, which increases the variable value by one.
Here is some code to count to ten:
<html>
<body>
<script type="text/vbscript">
for i = 1 to 10
document.write( i & " " )
next
</script>
</body>
</html>
Here we create the counter variable "i," and tell it to execute the document.write 10 times (for i= 1 to 10). Each time through the loop, the value in "i" is increased by1 until it reaches the number 10, at which point the loop is exited. Here is the result:
1 2 3 4 5 6 7 8 9 10
I can also print out text a certain amount of times. If I were at a wrestling match in the 1980s and the Iron Sheik came out, I could create a program to chant USA USA USA over and over again:
<html>
<body>
<script type="text/vbscript">
for i = 0 to 5
document.write("USA!")
next
</script>
</body>
</html>
The result:
USA! USA! USA! USA! USA! USA!
Stepping it Up
You can use the Step keyword to increase or decrease the value of the counter variable. If we wanted to increase the value by 5, we would use Step 5. If we wanted to decrease the value by 5 we would type Step -5. Here is an example. Prepare to have your minds blown!
<html>
<body>
<script type="text/vbscript">
for i = 2 to 8 Step 2
document.write(i & "<br />")
next
</script>
<p> Who do we appreciate?</p>
<p>Yamatasito Shitso Corp, Yamatasito Shitso Corp, YAY Yamatasito Shitso Corp!</p>
</body>
</html>
The result of our cheer leading code would be:
2
4
6
8
Who do we appreciate?
Yamatasito Shitso Corp, Yamatasito Shitso Corp, YAY Yamatasito Shitso Corp!
And now...a countdown:
<html>
<body>
<script type="text/vbscript">
for i = 10 to 0 Step -1
document.write("T-Minus " & i & "<br />")
next
</script>
<p><h1>Blastoff!</h1></p>
</body>
</html>
The result:
T-Minus 10
T-Minus 9
T-Minus 8
T-Minus 7
T-Minus 6
T-Minus 5
T-Minus 4
T-Minus 3
T-Minus 2
T-Minus 1
T-Minus 0
Blastoff!
And here, we will loop through some headers, Wizard of Oz style:
<html>
<body>
<script type="text/vbscript">
for i=1 to 6
document.write("<h" & i & ">I'm Melting!" & "</h" & i & ">")
next
</script>
<p><h2><i>What a cruel world when a little girl can destroy my beautiful wickedness!</i></h2></p>
</body>
</html>
The result:
I'm Melting!
I'm Melting!
I'm Melting!
I'm Melting!
I'm Melting!
I'm Melting!
What a cruel world when a little girl can destroy my beautiful wickedness!
Next: The For Each...Next Loop >>
More BrainDump Articles
More By James Payne