Controlling Program Flow - Microsoft® Visual Basic® Scripting Edition

> ### Controlling Program Flow in VBScript

> ##### Controlling Program Execution

> > Using conditional statements and looping statements (also known as control structures), you can write VBScript code that makes decisions and repeats actions.

> ##### Making Decisions Using If ... Then ... Else

> > The If ... Then ... Else statement is used to evaluate whether a condition is True or False and then to specify one or more statements to run, depending on the result. Usually, the condition is an expression that uses a comparison operator to compare one value or variable with another. For information about comparison operators, see Comparison Operators. If...Then...Else statements can be nested to as many levels as you need.

> > ##### Running Statements if a Condition Is True

> > If you need to run only one statement when a condition is True, you can use the single-line syntax of the If...Then...Else statement. The following example shows the single-line syntax; notice that this example omits the Else keyword.

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub FixDate()
> >          Dim myDate
> >          myDate = #2/13/95#
> >          If myDate < Now Then myDate = Now
> >      End Sub
> >     -->
> >     </SCRIPT>

> > If you want to run more than one line of code, you must use the multiple-line syntax. This syntax includes the End If statement, as shown in the following example:

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub AlertUser(value)
> >          If value = 0 Then
> >              AlertLabel.ForeColor = "Red"
> >              AlertLabel.Font.Bold = True
> >              AlertLabel.Font.Italic = True
> >          End If
> >      End Sub
> >     -->
> >     </SCRIPT>

> > ##### Running Certain Statements if a Condition Is True, and Running Others if it Is False

> > You can use an If...Then...Else statement to define two blocks of executable statements: one block to run if the condition is True, the other block to run if the condition is False.

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub AlertUser(value)
> >          If value = 0 Then
> >              AlertLabel.ForeColor = vbRed
> >              AlertLabel.Font.Bold = True
> >              AlertLabel.Font.Italic = True
> >          Else
> >              AlertLabel.Forecolor = vbBlack
> >              AlertLabel.Font.Bold = False
> >              AlertLabel.Font.Italic = False
> >          End If
> >      End Sub
> >     -->
> >     </SCRIPT>

> ##### Using Loops to Repeat 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
> > - While...Wend: Loops while a condition is True
> > - For...Next: Uses a counter to run statements a specified number of times

> ##### Using Do Loops

> > 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 first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example). In the ChkFirstWhile procedure, if myNum were set to 9 instead of 20, the statements inside the loop would never run. In the ChkLastWhile procedure, the statements inside the loop run only once because the condition is already False.

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      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
> >     -->
> >     </SCRIPT>

> > ##### Repeating a Statement Until a Condition Becomes True

> > 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 shown in the first example following this paragraph), or you can check it after the loop has run at least once (as shown in the second example). As long as the condition is False, the looping occurs.

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      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
> >     -->
> >     </SCRIPT>

> > ##### Exiting a Do ... Loop Statement from Inside the Loop

> > 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 is False, the loop runs as usual.

> > If...Then...Else statement checks for this condition, preventing the endless repetition.

> >     <SCRIPT LANGUAGE="VB">
> >     <!--
> >      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
> >     -->
> >     </SCRIPT>

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

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub DoMyProc50Times()
> >          Dim x
> >          For x = 1 To 50
> >              MyProc
> >          Next
> >      End Sub
> >     -->
> >     </SCRIPT>

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

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub TwosTotal()
> >          Dim j, total
> >          For j = 2 To 10 Step 2
> >              total = total + j
> >          Next
> >          MsgBox "The total is " & total
> >      End Sub
> >     -->
> >     </SCRIPT>

> > To decrease the counter variable, you use a negative Step value. When doing so, 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.

> >     <SCRIPT LANGUAGE="VBScript">
> >     <!--
> >      Sub NewTotal()
> >          Dim myNum, total
> >          For myNum = 16 To 2 Step -2
> >              total = total + myNum
> >          Next
> >          MsgBox "The total is " & total
> >      End Sub
> >     -->
> >     </SCRIPT>

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

> © 1996 by Microsoft Corporation.