51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

查看: 9352|回复: 7
打印 上一主题 下一主题

[求助] 怎样跳出while循环

[复制链接]

该用户从未签到

跳转到指定楼层
1#
发表于 2009-5-8 15:30:38 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
while XXXXX.exit
    AAA
    BBB   
    If YYYY.exist=False Then
       break      '跳出While循环
    End If
    CCC
    DDD
WEnd

在运行到break是出错,我试了exit, 也不行,哪位高手知道要怎么跳出while loop啊?
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏
回复

使用道具 举报

该用户从未签到

2#
发表于 2009-5-8 15:46:38 | 只看该作者
建议用do loop 然后exit do
回复 支持 反对

使用道具 举报

该用户从未签到

3#
发表于 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就可以了
回复 支持 反对

使用道具 举报

该用户从未签到

4#
 楼主| 发表于 2009-5-11 07:48:40 | 只看该作者

回复 2# 的帖子

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

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

[ 本帖最后由 7house 于 2009-5-11 08:34 编辑 ]
回复 支持 反对

使用道具 举报

该用户从未签到

5#
 楼主| 发表于 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吗?
回复 支持 反对

使用道具 举报

该用户从未签到

6#
 楼主| 发表于 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了.
回复 支持 反对

使用道具 举报

该用户从未签到

7#
发表于 2009-5-11 09:51:04 | 只看该作者
原帖由 7house 于 2009-5-11 08:50 发表
谢谢 Gerrard 和hsjzfling,在VBScript Programmer's Reference的书上找到的,while loop是没有强制跳出loop的code,唯一的控制语句就是while的那一句,我真是笨蛋,花了好多时间在不可能实现的事 ...


一起学习
回复 支持 反对

使用道具 举报

  • TA的每日心情
    开心
    2015-9-22 15:26
  • 签到天数: 16 天

    连续签到: 1 天

    [LV.4]测试营长

    8#
    发表于 2009-5-12 08:40:42 | 只看该作者
    一直很想要类似C里面的跳出当前循环的方法,但VB里面没有
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-5-10 07:37 , Processed in 0.072508 second(s), 27 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表