提前循环提前的方法
除了使用GOTO之外,还有什么方法可以放弃循环中的一段代码,将其推进到下一个循环? 在下面的代码中,GOTO是一种有效的方法吗?
Do While x < 10 a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) For n = 1 To 4 aa(i) = i * 2 + 4 bb(i) = i * 2 + 5 cc(i) = i * 2 + 6 Next If a = b Then GoTo lastline If a = c Then GoTo lastline If b = c Then GoTo lastline For i = 1 To 4 testa = (a = aa(i) Or a = bb(i) Or a = cc(i)) If testa Then GoTo lastline testb = (b = aa(i) Or b = bb(i) Or b = cc(i)) If testb Then GoTo lastline testc = (c = aa(i) Or c = bb(i) Or c = cc(i)) If testc Then GoTo lastline Next Debug.Print a & ","; b & ","; c lastline: x = x + 1 Loop
为了完整起见,如果你提取了一个Sub
来完成这个工作,这就是它的样子。 请注意,你有我的假设是你的原始代码中的一个错字:
For n = 1 To 4 aa(i) = i * 2 + 4 '<--- 'i'? shouldn't these be 'n'? bb(i) = i * 2 + 5 cc(i) = i * 2 + 6 Next
因为你以后使用i
作为循环计数器,它将永远是第一次通过循环时所进入的任何东西,以后每次都是4次。 我的猜测是,这不是你想要的。
那个出路…提取Sub
:
Private Sub WhateverYoureDoing(aa As Variant, bb As Variant, cc As Variant) Dim a As Long, b As Long, c As Long a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) 'Assuming this should be 'i' instead of 'n' as the loop count. For i = 1 To 4 aa(i) = i * 2 + 4 bb(i) = i * 2 + 5 cc(i) = i * 2 + 6 Next If a = b Then Exit Sub If a = c Then Exit Sub If b = c Then Exit Sub For i = 1 To 4 If a = aa(i) Or a = bb(i) Or a = cc(i) Then Exit Sub If b = aa(i) Or b = bb(i) Or b = cc(i) Then Exit Sub If c = aa(i) Or c = bb(i) Or c = cc(i) Then Exit Sub Next Debug.Print a & ","; b & ","; c End Sub
调用代码:
Do While x < 10 WhateverYoureDoing aa, bb, cc x = x + 1 Loop
请注意,您可以极大地简化此代码。 例如,这…
a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) '... If a = b Then GoTo lastline If a = c Then GoTo lastline If b = c Then GoTo lastline
…可以简单地说:
Do a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) Loop While a <> b And a <> c And b <> c
我只是用if语句来包装可能被跳过的部分。 第二个For循环也可以使用Exit For提前退出。 这样做可以避免使用GoTo。 我没有testing下面的代码,但它应该说明这个概念。
Do While x < 10 a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) For n = 1 To 4 aa(i) = i * 2 + 4 bb(i) = i * 2 + 5 cc(i) = i * 2 + 6 Next If Not a = b Or Not a = c Or Not b = c Then For i = 1 To 4 testa = (a = aa(i) Or a = bb(i) Or a = cc(i)) testb = (b = aa(i) Or b = bb(i) Or b = cc(i)) testc = (c = aa(i) Or c = bb(i) Or c = cc(i)) If testa Or testb Or testC Then Exit For Else If i = 4 Then Debug.Print a & ","; b & ","; c End If End If Next End If x = x + 1 Loop
如前所述,假如你使用IF来跳过:
Do While x < 10 a = Int(Rnd * 100) b = Int(Rnd * 100) c = Int(Rnd * 100) For n = 1 To 4 aa(i) = i * 2 + 4 bb(i) = i * 2 + 5 cc(i) = i * 2 + 6 Next If Not a = b Or Not a = c Or Not b = c Then For i = 1 To 4 testa = (a = aa(i) Or a = bb(i) Or a = cc(i)) If Not testa Then testb = (b = aa(i) Or b = bb(i) Or b = cc(i)) If Not testb Then testc = (c = aa(i) Or c = bb(i) Or c = cc(i)) If Not testc Then Debug.Print a & ","; b & ","; c End If End If End If Next End If x = x + 1 Loop
你可以用很多不同的方式使用Exit
,包括Exit For
, Exit For
For...Next
Loop。
这里是Exit
MSDN
退出
立即退出For循环。 执行继续执行Next语句后面的语句。 Exit For只能在For … Next或For Each … Next循环中使用。 在嵌套的For循环中使用时,Exit For退出最内层的循环,并将控制权转移到下一个更高级别的嵌套。