BrainDump
  Home arrow BrainDump arrow Page 3 - VBScript: Functions and Loops
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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

VBScript: Functions and Loops
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2007-12-17

    Table of Contents:
  • VBScript: Functions and Loops
  • Sub Procedures
  • Loop Flakes...They're Great!
  • The For Each...Next Loop
  • Do Loop
  • Do Until

  • 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


    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!

    More BrainDump Articles
    More By James Payne


     

    BRAINDUMP ARTICLES

    - Internet Explorer 8 Review
    - Nilpo`s Top Windows Add-Ons
    - Beginning Silverlight 2.0 Development using ...
    - Fixing Vista`s Troubles
    - Preparing Windows Images for Mass Deployment
    - The Trouble With Vista
    - Slipstreamed and Unattended Windows Installa...
    - Microsoft Office SharePoint Server
    - Microsoft Office SharePoint Designer
    - Microsoft Windows SharePoint Services 3.0
    - Microsoft Live Mesh Overview
    - XAML Brushes and Silverlight
    - Silverlight and XAML Basics
    - Immortal XP
    - XAML Basics





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT