工控随笔_20_西门子_WinCC的VBS脚本_09_常量和流程控制_02

  vbs不但提供了分支结构,还提供了丰富的循环形式。一共有3种循环:

  1、for循环

  2、do...loop循环

  3、while循环

  各种循环有各自的特点,在使用的时候可以进行转换。

  前面已经描述过For循环,这里简单的描述一下后面两种循环。

一、Do....loop循环

  

Option Explicit

'do loop 循环

'do loop循环有两种形式
'1、形式1 while形式,  while true 就一直循环
'2、形式2 until形式,  until true 就停止循环

Dim bLoopAgain

'while形式的循环
'只要循环条件是true逻辑结果,就一直循环
Do
    Dim nInput
    bLoopAgain = False
    
    nInput= InputBox("请输入数值:","while形式循环")
    If Not IsNumeric(nInput) Then
        bLoopAgain = True
    End If
Loop While bLoopAgain 

'until形式的循环
'只要循环条件为true,就结束循环
Do 
    bLoopAgain = false
    nInput = InputBox("请输入数值: ","until形式循环")
    If IsNumeric(nInput) Then
        bLoopAgain = True
    End If
Loop Until bLoopAgain 
    
    
'同时do循环的while关键字还可以放在最前面
'形成下面的格式
'do while 逻辑结果
    '循环语句
'loop

bLoopAgain = True
Do While bLoopAgain
    nInput = InputBox("请输入数值:","do while放在一起","")
    If IsNumeric(nInput) Then
        bLoopAgain = False
    End If
Loop

'exit do循环语句
'有时候循环次数执行过多就跳出循环,比方说多次输入的密码错误就不在执行
Dim strCipher
Dim nInputCount

nInputCount = 0
Do 
    bLoopAgain = True
    nInputCount = nInputCount + 1
    strCipher = InputBox("请输入密码:")
    If strCipher = "volcanol" Then
        bLoopAgain = False
    End If
    
    '如果输入密码的次数超过5次,那么就跳出循环
    If nInputCount = 5 Then
        MsgBox "输入密码错误超过5次,禁止登陆!",vbInformation,"提示"
        Exit Do
    End If
Loop While bLoopAgain 

、while循环

'while... wend 循环
'vbs中还有一个比较简洁的循环语句, while....wend
'这个循环当循环条件的逻辑结果为 true的时候一直循环
bLoopAgain = True 
While bLoopAgain 
    If "volcanol" = InputBox("请输入密码:","输入") Then
        bLoopAgain = False
    End If
Wend 

三、Tips

1、集合和数组遍历可以使用For循环

2、do循环要注意while和until的位置,两个需要注意

 

--------------------------------------------------------------分割线---------------------------------------------------------------

1、文章均为个人原创,欢迎转载,转载请保留出处:https://www.cnblogs.com/volcanol/

2、获取工控PLC、变频器、HMI、计算机、Windows、Linux、嵌入式资料点击:获取资料

3、如果您觉得文章对您有帮助可转至页面上半部分打赏,或移步:打赏 

4、或者在页面右下角点推荐哟!!!

--------------------------------------------------------------分割线---------------------------------------------------------------

猜你喜欢

转载自www.cnblogs.com/volcanol/p/10101502.html