VBScript7-VBScript Looping Statements

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...

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.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: VBScript7-VBScript Looping Statements
VBScript7-VBScript Looping Statements
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/10/vbscript7-vbscript-looping-statements.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/10/vbscript7-vbscript-looping-statements.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content