4.vbs的循环,switch,判断等语句

1.条件判断语句

If Then Else

Sub judge(x)
   If x = 0 Then
      MsgBox "the num is 0"
   ElseIf x = 1 Then
      MsgBox "the num is 1"
   ElseIf x = 2 Then 
      MsgBox "the num is 2"
   Else
      MsgBox "the num is other"
   End If      
End Sub
judge(0)

2.Select case 

Sub sel(x)
Select case x 
   case 1
        MsgBox "the num is 1"
   case 2
        MsgBox "the num is 2"
   case 3
        MsgBox "the num is 3"
   case 4
        MsgBox "the num is 4"         
End Select 
End Sub
sel(3)

3.当条件满足为true时执行,循环do loop,先判断再做

Sub loo(i)
do while i < 5
   MsgBox i
   i=i+1
loop
End Sub

4.当条件满足为true时执行,循环do loop,先做一次在判断

Sub loo(i)
do 
   MsgBox i
   i=i+1
loop while i < 5
End Sub
loo(6)

5.当条件为false执行,意思是直到i小于等于5都执行

Sub loo(i)
do until i > 5
   MsgBox i
   i=i+1
loop 
End Sub
loo(0)

6.先执行一次,当条件为false继续执行

do
   MsgBox i
   i=i+1
loop  until i > 5
End Sub 
loo(6)

7.exit do可以终止循环

do until i > 5
   MsgBox i
   i=i+1
   exit do
loop 
End Sub
loo(0)

8.while wend 不建议使用,用do loop

9.for next

 for i=0 to UBound(Remark) 
   ---    
 next

10.强行终止for,使用end for

for i = 0 to 10
MsgBox i
if i>5 Then
exit for
End If
next

11.在不知道数组长度的时候使用for each

dim names(2)
names(0) = "George"
names(1) = "John"
names(2) = "Thomas"
for each x in names
MsgBox x
next

也可以使用exit for强行终止

原文地址:https://www.cnblogs.com/caimuqing/p/5773761.html