![]() |
VBScript - Looping Through Code![]() ![]() ![]() ![]() ![]()
Using Loops to Repent Code
Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is False; others repeat statements until a condition is True. There are also loops that repeat statements a specific number of times. The following looping statements are available in VBScript:
• Do Loop: Loops while or until a condition is True. You can use Do ... Loop statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is True or until a condition becomes True. Repeating Statements While a Condition Is True Use the While keyword to check a condition in a Do ... Loop statement. You can check the condition before you enter the loop (as shown in the following ChkFirstWhile example), or you can check it after the loop has run at least once (as shown in the ChkLastWhile example). In the ChkFirstWhile procedure, if myNum is set to 9 instead of 20, the statements inside the loop will never run. In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False.
Sub ChkFirstWhile() Repeating a Statement Until a Condition Becomes Term You can use the Until keyword in two ways to check a condition in a Do ... Loop statement. You can check the condition before you enter the loop (as show## in the following ChkFirstUntil example), or you can check it after the loop has run at least once (as shown in the ChkLastUntil example). As long as the condition is False, the looping occurs.
Sub ChkFirstUntil() You can exit a Do ... Loop by using the Exit Do statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the Exit Do statement in the True statement block of an If ... Then ... Else statement. If the condition i's False, the loop runs as usual. In the following example, myNum' is assigned a value that creates an endless loop. The If ... Then ... Else statement checks for this condition, preventing the endless repetition.
Sub ExitExample () Using While ... Wend The While Wend statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in While ... Wend, it is recommended that you use Do ... Loop instead. Using For .. Next You can use For ... Next statements to run a block of statements a specific number of times. For loops, use a Counter variable whose value is increased or decreased with each repetition of the loop. For example, the following procedure causes a procedure called MyProc to execute 50 times. The For statement specifies the counter variable x and its start and end values. The Next statement increments the counter variable by 1.
Sub DoMyProc50Times() Using the Step keyword, you can increase or decrease the counter variable by the value you specify. In the following example, the counter variable j is incremented by 2 each time the loop repeats. When the loop is, finished,# total is the sum of 2, 4, 6, 8, and 10.
Sub TwosTotal() To decrease the counter variable, you use a negative Step value. You must specify an end value that is less than the start value. In the following example, the counter variable myNum is decreased by 2 each time the loop repeats. When the loop is finished, total is the sum of 16, 14, 12, 10, 8, 6, 4, and 2.
Sub NewTotal() Using For Each...Next A For Each ... Next loop is similar to a For ... Next loop. Instead of repeating the statements a specified number of times, a For Each ... Next loop repeats a group of statements for each item in a collection of objects or for each element of an array. This is especially helpful if you don't know how many elements are in a collection. You can exit any For ... Next statement before the counter reaches its end value by using the Exit For statement. Because you usually want to exit only in certain situations, such as when an error occurs, you should use the Exit For statement in the True statement block of an If..,.Then ... Else statement. If the condition is False, the loop runs as usual. In the following HTML code example, the contents of a Dictionary object is used to place text in several text boxes:
<HTML> Setd=CreateObject("Scripting.pictionary") For Each I in d
|
|
Domain NamesVBSCRIPT VBScript Code VBScript Data Types VBScript Variables constants VBScript Operators Using Conditional Statements Looping Statements in VBScript VBScript Procedures VBScript Coding Conventions VBScript and Forms
Using VBScript with ObjectsWeb DesignWeb HostingE Commerce |
| Home | Web Hosting | Web Design | Sitemap |
| Copyright (C) 2007. Web Domain design hosting. All rights reserved. |