这篇主要介绍VBScript流程控制,包括分支和循环两个方面。
分支结构:
分支就是在代码中做出判断,然后根据判断结果,有选择的执行部分代码。VBScript中分支有两种结构:If---End If以及Select-----End Select
1. If分支
其最简形式:
If <expression> Then
<other code goes here>
End If
其中<expression>中可以使用任何结果是True或False的语句(也就是布尔表达式)。
<other code goes here>中可以包含多个ElseIf和Else,也可以是多个If---End If嵌套:
If <expression1> Then
If <expression2> Then
<code goes here 1>
ElseIf <expression3> Then
<code goes here 2>
Else
<code goes here 3>
End If
Else
<other code goes here>
End If
2. Select
Case分支
Select Case分支与If分支的区别是:Select----End Select结构是处理同一个表达式的不同的值时使用。而If分支可以对不同的表达式求值,也就是说If分支能够完成Select Case分支完成的任务,但是Select Case分支不能完成If分支完成的任务,但是对于同一表达式求不同值时的处理使用Select Case效率更高。
Select Case结构:
Select Case <expression>
Case <possibility 1>
<code goes here 1>
Case <possibility 2>
<code goes here 2>
------
Case <possibility n>
<code goes here n>
Case Else
<other code goes here>
End Select
当然多个Select Case分支可以相互嵌套,也可以与If分支相互嵌套。
循环结构:
循环主要用于反复执行同一段代码。循环结构主要有四种:
For ----Next
Do loop
For Each --- Next
While--- Wend
下面分别来看看各循环结构:
1. For
---Next循环
主要适用于:代码反复执行的次数已知或对复杂数据结构中的每个元素执行一段代码。
Option Explicit
Dim lngIndex
For lngIndex = 1 To 5
MsgBox "Loop Index: " & lngIndex
Next
‘依次打印出:1,2,3,4,5
我们也可以用Step关键字跳过某些数字:
Option Explicit
Dim lngIndex
For lngIndex = 1 To 5 Step 2
MsgBox "Loop Index: " & lngIndex
Next
‘依次打印出:1,3,5
Step关键字设定的是任意你想要的步幅。也可以用Step关键字逆向的执行循环,即后面接负值,但要注意此时循环索引的初始值应比结束值大。
2. For
Each----Next循环
该循环是一个特殊的循环,专门用于遍历集合,如数组,集合中通常含有同一类型的对象。For Each --- Next不能直接控制循环次数,循环次数取决于您所遍历的集合中对象的数量。
Option Explicit
Dim strElement
Dim astrColors()
astrColors(0) = "Red"
astrColors(1) = "Green"
astrColors(2) = "Blue"
For Each strElement In astrColors
MsgBox strElement
Next
‘依次打印出:Red,Green,Blue
3. Do
loop循环
Do循环是最常用的循环结构,其威力来至于While和Until关键字,While和Until关键字既可以用在循环的开始也可以用在循环的结尾来控制是否要再次执行循环。
看看While的使用:
Option Explicit
Dim boolLoopAgain
Dim lngLoopCount
Dim strResponse
boolLoopAgain = False
lngLoopCount = 0
Do
boolLoopAgain = False
lngLoopCount = lngLoopCount + 1
strResponse = InputBox ("What is the magic word?")
If UCase(Trim(strResponse)) = "PLEASE" Then
MsgBox "Correct! Congratulations"
Else
If lngLoopCount < 5 Then
MsgBox "Sorry,try again."
boolLoopAgain = True
Else
MsgBox "OKay,the word we wanted was Please."
End If
End If
Loop While boolLoopAgain
只有在While语句中的表达式为True时循环才会再次执行,要注意的是因为没有在Do语句中设置条件,所以循环中的代码至少会执行一次(即先执行一遍然后再判断)。这也是为什么我们把strResponse = InputBox ("What is the magic word?")语句放在Do里面。当然我们也可以把While放在Do后面(因为我们的目的是至少是循环执行一次,所以要把boolLoopAgain的初始值变化为True):
Option Explicit
Dim boolLoopAgain
Dim lngLoopCount
Dim strResponse
boolLoopAgain = True
lngLoopCount = 0
Do While boolLoopAgain
boolLoopAgain = True
lngLoopCount = lngLoopCount + 1
strResponse = InputBox ("What is the magic word?")
If UCase(Trim(strResponse)) = "PLEASE" Then
MsgBox "Correct! Congratulations"
Else
If lngLoopCount < 5 Then
MsgBox "Sorry,try again."
boolLoopAgain = False
Else
MsgBox "OKay,the word we wanted was Please."
End If
End If
Loop
上面这种使用因为Do后设置了条件,会先进行判断然后再执行,如不满足设置的条件就不会执行Do循环中的语句。选择While的位置,取决于我们是否要使Do循环至少执行一次,如果是就放在循环的末尾,如果否就放在循环的开始。
Until的使用跟While相同,功能也相同,主要的区别在于语义:当While某一条件为True时,反复执行一段代码,或者反复执行一段语句,直到(until)某一条件为True。
有点绕,看看细细品味就可以找出两种的差别,看个例子,跟上面的例子完成相同的功能:
Option Explicit
Dim boolLoopAgain
Dim lngLoopCount
Dim strResponse
boolLoopAgain = True
lngLoopCount = 0
Do
boolLoopAgain = True
lngLoopCount = lngLoopCount + 1
strResponse = InputBox ("What is the magic word?")
If UCase(Trim(strResponse)) = "PLEASE" Then
MsgBox "Correct! Congratulations"
Else
If lngLoopCount < 5 Then
MsgBox "Sorry,try again."
boolLoopAgain = False
Else
MsgBox "OKay,the word we wanted was Please."
End If
End If
Loop Until boolLoopAgain
仔细对比以上代码和使用While时完成相同功能的代码,注意boolLoopAgain的初始赋值,可以更好的理解两则的语义区别。
4. While---wend循环
While---wend循环是一种老式语法,不怎么常用,当然看个人爱好了。看个简单的例子:
Option Explicit
Dim lngCounter
lngCounter = 0
While lngCounter <= 5
lngCounter = lngCounter + 1
MsgBox lngCounter
Wend
跳出循环:
跟跳出过程和函数一样,我们也可以使用Exit关键字跳出循环:
Exit For
Exit Do
要注意的是While---Wend不能强制跳出循环。
写于2012-12-16 没翅膀的飞鱼