VBScript Looping Statements

|  | Search W3Schools : |

HOME | HTML | CSS | XML | JAVASCRIPT | ASP | PHP | SQL | MORE... |  | References | Examples | Forum | About

ADVERTISEMENTS XML Certification Download XML editor Custom Programming |  |

Table of contents

VBScript Tutorial
VB HOME
VB Introduction
VB How to
VB Where to
VB Variables
VB Procedures
VB Conditional
VB Looping
VB Summary
Examples
VB Examples
References
VB Functions
VB Keywords

Selected Reading

Web Statistics
Web Glossary
Web Hosting
Web Quality
W3Schools Tutorials
W3Schools Forum
Helping W3Schools

# VBScript Looping Statements

## Examples

For...next loop
This example demonstrates how to make a simple For....Next loop.

Looping through headers
This example demonstrates how you can loop through the 6 headers in html.

For...each loop
This example demonstrates how to make a simple For.....Each loop.

Do...While loop
This example demonstrates how to make a simple Do...While loop.

## Looping Statements

Very often when you write code, you want to allow the same block of code to run a number of times. You can use looping statements in your code to do this.

In VBScript we have four looping statements:

- For...Next statement - runs statements a specified number of times.
- For Each...Next statement - runs statements for each item in a collection or each element of an array
- Do...Loop statement - loops while or until a condition is true
- While...Wend statement - Do not use it - use the Do...Loop statement instead

## For...Next Loop

You can use a For...Next statement to run a block of code, when you know how many repetitions you want.

You can use a counter variable that increases or decreases with each repetition of the loop, like this:

    For i=1 to 10
      some code
    Next

The For statement specifies the counter variable (i) and its start and end values. The Next statement increases the counter variable (i) by one.

### Step Keyword

Using the Step keyword, you can increase or decrease the counter variable by the value you specify.

In the example below, the counter variable (i) is increased by two each time the loop repeats.

    For i=2 To 10 Step 2
      some code
    Next

To decrease the counter variable, you must use a negative Step value. You must specify an end value that is less than the start value.

In the example below, the counter variable (i) is decreased by two each time the loop repeats.

    For i=10 To 2 Step -2
      some code
    Next

### Exit a For...Next

You can exit a For...Next statement with the Exit For keyword.

## For Each...Next Loop

A For Each...Next loop repeats a block of code for each item in a collection, or for each element of an array.

    dim cars(2)
    cars(0)="Volvo"
    cars(1)="Saab"
    cars(2)="BMW"

    For Each x in cars
      document.write(x & "<br />")
    Next

## Do...Loop

You can use Do...Loop statements to run a block of code when you do not know how many repetitions you want. The block of code is repeated while a condition is true or until a condition becomes true.

### Repeating Code While a Condition is True

You use the While keyword to check a condition in a Do...Loop statement.

    Do While i>10
      some code
    Loop

If i equals 9, the code inside the loop above will never be executed.

    Do
      some code
    Loop While i>10

The code inside this loop will be executed at least one time, even if i is less than 10.

### Repeating Code Until a Condition Becomes True

You use the Until keyword to check a condition in a Do...Loop statement.

    Do Until i=10
      some code
    Loop

If i equals 10, the code inside the loop will never be executed.

    Do
      some code
    Loop Until i=10

The code inside this loop will be executed at least one time, even if i is equal to 10.

### Exit a Do...Loop

You can exit a Do...Loop statement with the Exit Do keyword.

    Do Until i=10
      i=i-1
      If i<10 Then Exit Do
    Loop

The code inside this loop will be executed as long as i is different from 10, and as long as i is greater than 10.

Ektron CMS400.NET Version 7.6 delivers all of the flexibility and features you need to deploy the Web site you want, quickly and efficiently. learn more...

6 ways to take your site to the next level with Ektron:

| Create site stickiness through social networking. Keep it personal, relevant and interactive and they'll come back for more.
| Keep it open. Your site needs to be ready and able to connect to outside services. Ektron's open API gives you maximum flexibility.
| Streamline content and document management. Users need to quickly and intuitively find and add information.
| Empower your content authors. Reduce IT bottlenecks by allowing business users to create and edit Web content and forms.
| Climb to the top of search rankings. SEO tools, URL aliasing and eCommerce for your digital marketing strategy
| Add powerful Web 2.0 tools like blogs, wikis, forums, geo-mapping, rating systems and RSS feeds easily.

See why there are 20,000+ Ektron integrations worldwide. Request an INSTANT DEMO or download a FREE TRIAL today.

WEB HOSTING
Domain Name Registration & More!
$15 Domain Name Registration Save $20 / year!
Buy UK Domain Names Register Domain Names
Cheap Domain Names Cheap Web Hosting
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting

WEB BUILDING
Website Templates Flash Templates Website Builder Internet Business Opportunity
Custom Programming
FREE Trial or Demo Web Content Manager Forms,Web Alerts,RSS
Download XML editor
FREE Flash Website
FREE Web Templates

EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification

HOME or Top of Page |  | Printer Friendly
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy. Copyright 1999-2009 by Refsnes Data. All Rights Reserved.