BrainDump
  Home arrow BrainDump arrow Page 2 - Looping Statements in VBScript
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

Looping Statements in VBScript
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 5
    2007-12-11

    Table of Contents:
  • Looping Statements in VBScript
  • Incremental Loops
  • Conditional Loops
  • More On Loops

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Looping Statements in VBScript - Incremental Loops


    (Page 2 of 4 )

    VBScript uses the For…Next structure for creating incremental loops.  There are a few keyword variations that allow some flexibility in how the loop is executed.

    For variable = intStart To intEnd [Step intIncrement]

          ' Code to be repeated

    Next

    A basic For…Next statement is used to repeat a section of code a definite number of times. The following code will count to 5 and print each number.

    For x = 1 To 5

       WScript.Echo x

    Next

    The For statement requires that you supply a counter variable such as “x.” You must also supply its starting and ending value. The Next statement will increment the counter variable and repeat the enclosed code until it is no longer within the specified range, resulting in an output like the following:

    1

    2

    3

    4

    5

    The For statement also accepts the Step keyword which can be used to change the increment value. You may supply a negative number to cause the counter to decrement. The following example will list all even numbers from 10 to 0.

    For x = 10 To 0 Step -2

       Wscript.Echo x

    Next

    Make sure that your starting value is less than your ending value when incrementing and greater than it when decrementing in order to prevent errors.  The second output looks like this:

    10

    8

    6

    4

    2

    0

    Another variation of this loop is the For Each…Next loop.  This Each keyword will iterate through each element in a dictionary object, array, or collection.

    arrWords = Array("one", "two", "three", "four", "five")

     

    For Each strWord In arrWords

       WScript.Echo strWord

    Next

    In this example, I’ve created a simple string array. Using the For Each statement, I’ve iterated through each element without having to specify the number explicitly. The first variable in this statement must be a reference to the array element followed by the In keyword and a variable representing the array. The resulting output makes this very evident.

    one

    two

    three

    four

    five

    This same output can be generated using a simple For loop, however, the code is much more complex.

    For i = 0 To UBound(arrWords)

       strWord = arrWords(i)

       Wscript.Echo strWord

    Next

    In this example, I’ve had to programmatically determine the number of elements in the array and then make a reference to each of them.  The For Each syntax is a much cleaner, quicker approach.

    More BrainDump Articles
    More By Nilpo


     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek