Quackit Logo
$1.99 Domain Names
With every new non-domain purchase thru ZappyHost, you get a domain name for only $1.99.
Create free Flash websites

VBScript For Loop

Print Version

The VBScript For Loop allows you to execute code for a given number of iterations. Using a For loop, you provide a given expression, and the loop will iterate for as long as that expression results to true.

Example For Loop

Using the same scenario as the previous lesson, the loop will iterate from 0 to 8. It will then continue processing the rest of the page.

<script type="text/vbscript">
  For hoursWorked = 0 to 8
    document.write("Hours worked: " & hoursWorked)
    document.write("<br />")
  Next
    document.write("Home time!")
</script>

This results in:

Hours worked: 0
Hours worked: 1
Hours worked: 2
Hours worked: 3
Hours worked: 4
Hours worked: 5
Hours worked: 6
Hours worked: 7
Hours worked: 8
Home time!

For Each Loop

The VBScript For Each Loop allows you to iterate over an array. This can be a quick and easy way of accessing all elements within an array without you needing to know how many elements are in it.

<script type="text/vbscript">
  Dim shoppingList(3)
  shoppingList(0) = "Bananas"
  shoppingList(1) = "Bread"
  shoppingList(2) = "Pasta"
  For Each listItem In shoppingList
  document.write(listItem)
  document.write("<br />")
Next
</script>

This results in:

Bananas
Bread
Pasta

Enjoy this website?

  1. Add this page to your Favorites
  2. Link to this page (copy/paste into your own website or blog):
  3. Help support Quackit by making a donation

Oh, and thank you for supporting Quackit!

FREE Hosting!
With every domain you register with ZappyHost you get FREE hosting.
Need Website Content?
Get unique, quality digital content for your website.
  • 270+ Website Templates
  • 800+ Flash Templates
  • 25,000+ Images, Logos
  • 30,000+ e-Books
  • 15,000+ Scripts
  • 27,000+ Animated GIFs
  • 21,000+ Ringtones
  • ...and much more!
Get your content now!
© Copyright 2000 - 2010 Quackit.com