📄 vbs10.htm
字号:
<HTML>
<HEAD>
<TITLE>Looping Through Code</TITLE>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso8859-1">
<META NAME="MS.LOCALE" CONTENT="EN-US">
<META NAME="PRODUCT" CONTENT="Visual Basic Scripting Edition">
<META NAME="TECHNOLOGY" CONTENT="SCRIPTING">
<META NAME="CATEGORY" CONTENT="Tutorial">
<META NAME="Description" CONTENT="Using VBScript Looping Statements">
</HEAD>
<BODY BGCOLOR=FFFFFF LINK=#0033CC>
<!--TOOLBAR_START-->
<!--TOOLBAR_EXEMPT-->
<!--TOOLBAR_END-->
<FONT FACE="Verdana, Arial, Helvetica" SIZE=2>
<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%><TR VALIGN=TOP><TD WIDTH=360>
<FONT SIZE=1 COLOR=#660033>Microsoft® Visual Basic® Scripting Edition</FONT><BR>
<FONT SIZE=5 COLOR=#660033><B>Looping Through Code</B></FONT>
</TD>
<TD ALIGN=RIGHT>
<FONT SIZE=2> <A HREF="vbstutor.htm">VBScript Tutorial</A> <BR> <A HREF="vbs4.htm">Previous</A> | <A HREF="vbs14.htm">Next</A> <P></FONT>
</TD></TR>
</TABLE>
<P>
<HR NOSHADE SIZE=1>
<H5>Using Loops to Repeat Code</H5>
<BLOCKQUOTE>Looping allows you to run a group of statements repeatedly. Some loops repeat statements until a condition is <B>False</B>; others repeat statements until a condition is <B>True</B>. There are also loops that repeat statements a specific number of times.<P>
The following looping statements are available in VBScript:
<UL>
<LI><A HREF="vbs573.htm"><B>Do...Loop</B></A>: Loops while or until a condition is <B>True</B>.
<LI><A HREF="vbs604.htm"><B>While...Wend</B></A>: Loops while a condition is <B>True</B>.
<LI><A HREF="vbs579.htm"><B>For...Next</B></A>: Uses a counter to run statements a specified number of times.
<LI><A HREF="vbs581.htm"><B>For Each...Next</B></A>: Repeats a group of statements for each item in a collection or each element of an array.
</UL>
</BLOCKQUOTE>
<H5>Using Do Loops</H5>
<BLOCKQUOTE>
You can use <B>Do...Loop</B> statements to run a block of statements an indefinite number of times. The statements are repeated either while a condition is <B>True</B> or until a condition becomes <B>True</B>.
<H5>Repeating Statements While a Condition is True</H5>
Use the <B>While </B>keyword to check a condition in a <B>Do...Loop</B> 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 <FONT FACE="Courier New" SIZE=2>myNum</FONT> 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 <B>False</B>.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ChkFirstWhile()
Dim counter, myNum
counter = 0
myNum = 20
Do While myNum > 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
Sub ChkLastWhile()
Dim counter, myNum
counter = 0
myNum = 9
Do
myNum = myNum - 1
counter = counter + 1
Loop While myNum > 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
</FONT></PRE></BLOCKQUOTE>
<H5>Repeating a Statement Until a Condition Becomes True</H5>
You can use the <B>Until</B> keyword in two ways to check a condition in a <B>Do...Loop</B> statement. You can check the condition before you enter the loop (as shown 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 <B>False</B>, the looping occurs.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ChkFirstUntil()
Dim counter, myNum
counter = 0
myNum = 20
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
Sub ChkLastUntil()
Dim counter, myNum
counter = 0
myNum = 1
Do
myNum = myNum + 1
counter = counter + 1
Loop Until myNum = 10
MsgBox "The loop made " & counter & " repetitions."
End Sub
</FONT></PRE></BLOCKQUOTE>
<H5>Exiting a Do...Loop Statement from Inside the Loop</H5>
You can exit a <B>Do...Loop</B> by using the <B>Exit Do</B> statement. Because you usually want to exit only in certain situations, such as to avoid an endless loop, you should use the <B>Exit Do</B> statement in the <B>True</B> statement block of an <B>If...Then...Else</B> statement. If the condition is <B>False</B>, the loop runs as usual.<P>
In the following example, <FONT FACE="Courier New" SIZE=2>myNum</FONT> is assigned a value that creates an endless loop. The <B>If...Then...Else</B> statement checks for this condition, preventing the endless repetition.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub ExitExample()
Dim counter, myNum
counter = 0
myNum = 9
Do Until myNum = 10
myNum = myNum - 1
counter = counter + 1
If myNum < 10 Then Exit Do
Loop
MsgBox "The loop made " & counter & " repetitions."
End Sub
</FONT></PRE></BLOCKQUOTE>
</BLOCKQUOTE>
<H5>Using While...Wend</H5>
<BLOCKQUOTE>
The <B>While...Wend</B> statement is provided in VBScript for those who are familiar with its usage. However, because of the lack of flexibility in <B>While...Wend</B>, it is recommended that you use <B>Do...Loop</B> instead.
</BLOCKQUOTE>
<H5>Using For...Next</H5>
<BLOCKQUOTE>
You can use <B>For...Next</B> 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.<P>
For example, the following procedure causes a procedure called <FONT FACE="Courier New" SIZE=2>MyProc</FONT> to execute 50 times. The <B>For</B> statement specifies the counter variable x and its start and end values. The <B>Next</B> statement increments the counter variable by 1.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub DoMyProc50Times()
Dim x
For x = 1 To 50
MyProc
Next
End Sub
</FONT></PRE></BLOCKQUOTE>
Using the <B>Step</B> 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.
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub TwosTotal()
Dim j, total
For j = 2 To 10 Step 2
total = total + j
Next
MsgBox "The total is " & total
End Sub
</FONT></PRE></BLOCKQUOTE>
To decrease the counter variable, you use a negative <B>Step</B> value. You must specify an end value that is less than the start value. In the following example, the counter variable <FONT FACE="Courier New" SIZE=2>myNum</FONT> 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.<P>
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> Sub NewTotal()
Dim myNum, total
For myNum = 16 To 2 Step -2
total = total + myNum
Next
MsgBox "The total is " & total
End Sub
</FONT></PRE></BLOCKQUOTE>
You can exit any <B>For...Next</B> statement before the counter reaches its end value by using the <B>Exit For</B> statement. Because you usually want to exit only in certain situations, such as when an error occurs, you should use the <B>Exit For</B> statement in the <B>True</B> statement block of an <B>If...Then...Else</B> statement. If the condition is <B>False</B>, the loop runs as usual.<P>
</BLOCKQUOTE>
<H5>Using For Each...Next</H5>
<BLOCKQUOTE>
A <b>For Each...Next</b> loop is similar to a <b>For...Next</b> loop. Instead of repeating the statements a specified number of times, a <B>For Each...Next</B> 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.<p>
In the following HTML code example, the contents of a <B>Dictionary</B> object is used to place text in several text boxes:
<BLOCKQUOTE><PRE><FONT FACE="Courier New" SIZE=3> <HTML>
<HEAD><TITLE>Forms and Elements</TITLE></HEAD>
<SCRIPT LANGUAGE="VBScript">
<!--
Sub cmdChange_OnClick
Dim d 'Create a variable
Set d = CreateObject("Scripting.Dictionary")
d.Add "0", "Athens" 'Add some keys and items
d.Add "1", "Belgrade"
d.Add "2", "Cairo"
For Each I in d
Document.frmForm.Elements(I).Value = D.Item(I)
Next
End Sub
-->
</SCRIPT>
<BODY>
<CENTER>
<FORM NAME="frmForm"
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Text"><p>
<Input Type = "Button" NAME="cmdChange" VALUE="Click Here"><p>
</FORM>
</CENTER>
</BODY>
</HTML>
</FONT></PRE></BLOCKQUOTE>
</BLOCKQUOTE>
<hr noshade size=1>
<p align=center><em><a href="../../common/colegal.htm">© 1997 by Microsoft Corporation. All rights reserved.</a></em></p>
</FONT>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -