BrainDump
  Home arrow BrainDump arrow Page 3 - 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 - Conditional Loops


    (Page 3 of 4 )

    Conditional loops are constructed using the Do…Loop statements. VBScript provides the While and Until keywords to determine whether a loop should execute when a condition is true or false, respectively.

    The conditional statement can be any statement or calculation that evaluates to either True or False. It can even be as simple as a Boolean value.

    As you will see, the syntax of the Do…Loop statement is very flexible. I’ll demonstrate the most widely accepted versions first.

    Do While | Until statement

          ' Code to be repeated

    Loop

    You must supply either the While or Until keyword followed by a conditional statement. Any conditional statement will work as long as it can be evaluated to either True or False. Comparisons and equality tests are the most common type of conditional statements found in Do loops.

    x = 1

    Do While x <> 5

       WScript.Echo x

       x = x + 1

    Loop

    The code above will continue outputting the value of x as long as it does not equal 5.  In other words it will execute “While x does not equal 5”.  This continues to execute as long as the condition is met providing the following output:

    1

    2

    3

    4

    To see how the Until keyword processes false statements, we can modify the conditional statement above to its converse.

    x = 1

    Do Until x = 5

       WScript.Echo x

       x = x + 1

    Loop

    This loop will continue to execute “Until x equals 5.” A closer look shows that this loop will only continue to execute as long as the condition evaluates to false.

    x = 1

    Do Until x = 5

       WScript.Echo x

       x = x + 2

    Loop

    Take care when constructing your conditions.  Since x can never equal 5, the above code creates an endless or “infinite” loop because the condition can never be met.

    The alternative Do…Loop syntax places the keyword and conditional statement in the closing statement instead.  While this is acceptable, it’s not widely used.

    x = 1

    Do

       WScript.Echo x

       x = x + 1

    Loop Until x = 5

    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 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek