for..next双重循环的内层有exit for语句,怎样才能终止外循环

2025-02-28 14:59:52
推荐回答(3个)
回答1:

Sub Main()
Dim i, j As Integer

For i = 1 To 10
For j = 1 To 10
If j > 5 Then
GoTo Label
End If
Console.WriteLine("i = {0}, j = {1}", i, j)
Next
Next
Label:
Console.WriteLine("双重循环结束!")
End Sub

回答2:

for i=1 to n
for j=1 to m
…………
if …… then
结束=true
exit for
endif
next j
if 结束 then exit for
next i

回答3:

方案1:
Private Sub Command1_Click()
Dim f As Boolean
f = False
For i = 0 To 1000
For j = 0 To 1000
If i * j = 2000 Then
f = True
Exit For
End If
Next j
If f = True Then Exit For
Next i
Print i; j
End Sub

方案2:
Private Sub Command2_Click()
For i = 0 To 1000
For j = 0 To 1000
If i * j = 2000 Then GoTo JMP
Next j
Next i
JMP:
Print i; j
End Sub