BrainDump
  Home arrow BrainDump arrow Page 4 - 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 - More On Loops


    (Page 4 of 4 )

    There is one final looping method that provides a conditional loop. The While…Wend provides a shorthand for the Do While…Loop syntax.  However, since it is less flexible its use has been depreciated.  Here’s an example of our first conditional loop:

    x = 1

    While x <> 5

       WScript.Echo x

       x = x + 1

    Wend

    A While…Wend loop will only continue to execute as long as its conditional statement evaluates to True.

    From time to time you may wish to end the execution of a loop before it completes.  This can be done using the Exit statement.  Typically this will be used in conjunction with an IF statement to exit execution when an outside condition is met.

    For x = 1 To 10

       WScript.Echo x

       If x = 5 Then

           Exit For

       End If

    Next

     

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

       "six", "seven", "eight", "nine", "ten")

    For Each strWord In arrWords

       WScript.Echo strWord

       If strWord = "five" Then

           Exit For

       End If

    Next

     

    x = 1

    Do While x <> 10

       WScript.Echo x

       If x = 5 Then

           Exit Do

       End If

       x = x + 1

    Loop

    Each of the loops above has been modified so that it exits before execution completes. The output provided by these loops is the same as the previous examples even though the conditions have been changed to provide a much wider range of numbers.

    1

    2

    3

    4

    5

    one

    two

    three

    four

    five

    1

    2

    3

    4

    5

    Learning to use loops effectively can greatly increase the flexibility and power of your scripts.  Be sure to take the time to try nesting and combining your loops.  This can open up whole new avenues of scripting techniques for you to use.  Until next time, keep coding!


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

     

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