7house 发表于 2009-5-8 15:30:38

怎样跳出while循环

while XXXXX.exit
    AAA
    BBB   
    If YYYY.exist=False Then
       break    '跳出While循环
    End If
    CCC
    DDD
WEnd

在运行到break是出错,我试了exit, 也不行,哪位高手知道要怎么跳出while loop啊?

Gerrard 发表于 2009-5-8 15:46:38

建议用do loop 然后exit do

hsjzfling 发表于 2009-5-8 15:50:13

有问题,先查帮助

Exits a block of Do...Loop, For...Next, Function, or Sub code.

Exit Do
Exit For
Exit Function
Exit Property
Exit Sub
The Exit statement syntax has these forms:

Statement Description
Exit Do Provides a way to exit a Do...Loop statement. It can be used only inside a Do...Loop statement. Exit Do transfers control to the statement following the Loop statement. When used within nested Do...Loop statements, Exit Do transfers control to the loop that is one nested level above the loop where it occurs.
Exit For Provides a way to exit a For loop. It can be used only in a For...Next or For Each...Next loop. Exit For transfers control to the statement following the Next statement. When used within nested For loops, Exit For transfers control to the loop that is one nested level above the loop where it occurs.
Exit Function Immediately exits the Function procedure in which it appears. Execution continues with the statement following the statement that called the Function.
Exit Property Immediately exits the Property procedure in which it appears. Execution continues with the statement following the statement that called the Property procedure.
Exit Sub Immediately exits the Sub procedure in which it appears. Execution continues with the statement following the statement that called the Sub.

The following example illustrates the use of the Exit statement:

Sub RandomLoop
   Dim I, MyNum
   Do   ' Set up infinite loop.
      For I = 1 To 1000   ' Loop 1000 times.
         MyNum = Int(Rnd * 100)   ' Generate random numbers.
         Select Case MyNum   ' Evaluate random number.
            Case 17: MsgBox "Case 17"
               Exit For   ' If 17, exit For...Next.
            Case 29: MsgBox "Case 29"
               Exit Do   ' If 29, exit Do...Loop.
            Case 54: MsgBox "Case 54"
               Exit Sub   ' If 54, exit Sub procedure.
            End Select
      Next
   Loop
End Sub


根据帮助中的例子,把你的while语句改为do loop,再使用Exit Do就可以了

7house 发表于 2009-5-11 07:48:40

回复 2# 的帖子

你的意思是只能用Do loop吗?

While loop 是没有跳出循环CODE的吗?

[ 本帖最后由 7house 于 2009-5-11 08:34 编辑 ]

7house 发表于 2009-5-11 08:07:22

回复 3# 的帖子

在我的while loop中, 用while检查一个link是否存在,如果存在就执行,如果不存在就不执行了while loop里面的东西.那个link可能不存在,也可能有很多个.
如果用DO的话, 一开始是不检查的,要执行第一个loop后再检查.
这样的情况,一开始我觉得用While loop加 break 最简单,但没想到break不好使.
用FOR和DO就要再一个if在前面,就变成double check了.
是不是一定要这么麻烦呀?
真的就没有办法直接跳出while loop吗?

7house 发表于 2009-5-11 08:50:14

自己找答案 

谢谢 Gerrard 和hsjzfling,在VBScript Programmer's Reference的书上找到的,while loop是没有强制跳出loop的code,唯一的控制语句就是while的那一句,我真是笨蛋,花了好多时间在不可能实现的事情上:$ .

"Unlike the Do loop, you do not have the option of using either While or Until, nor can you place the condition at the end of the loop. The condition for whether to loop again can only be placed at the beginning of the loop, as you see here. Finally, a significant limitation of the While…Wend loop is that there is no equivalent to Exit For or Exit Do, meaning you cannot forcibly break out of the loop." - VBScript Programmer's Reference

看来要重新设计while loop了.

Gerrard 发表于 2009-5-11 09:51:04

原帖由 7house 于 2009-5-11 08:50 发表 http://bbs.51testing.com/images/common/back.gif
谢谢 Gerrard 和hsjzfling,在VBScript Programmer's Reference的书上找到的,while loop是没有强制跳出loop的code,唯一的控制语句就是while的那一句,我真是笨蛋,花了好多时间在不可能实现的事 ...

一起学习:handshake

virgolong 发表于 2009-5-12 08:40:42

一直很想要类似C里面的跳出当前循环的方法,但VB里面没有:Q
页: [1]
查看完整版本: 怎样跳出while循环