exit for 怎么连跳两级for each 了?好奇怪!!

lmhcs 2019-05-30 11:29:59
今天调试下程序时,忽然发现,exit for 一下子跳出两层的for。eixt for 不是只跳所在的那一层而已吗?蒙了。见下面代码

'筛选符合条件
For Each s As String In arr 'for1
If String.IsNullOrEmpty(s) Then Continue For
Dim nextFiller As Boolean = False
For Each b As fhBuilding In buildings 'for2
If nextFiller Then
nextFiller = False
Exit For
End If
For Each r As fhRoom In b.Rooms 'for3
If String.Equals(s, r.FullName, StringComparison.OrdinalIgnoreCase) Then
_VisRooms.Add(r)
nextFiller = True
Exit For '为什么exit for 不是离开for3 而是离开for2,直接跳到for1呢?
End If
Next
Next
Next



为了验证exit for 跳跃能力,特写了下面代码,发现exit for 就只能跳出所在的for 层,但为什么上面代码一下子跳出两层呢?


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a1() As Integer = New Integer() {1, 2, 3, 4, 5}
Dim a2() As Integer = New Integer() {11, 12, 13, 14, 15}
Dim a3() As Integer = New Integer() {21, 22, 23, 24, 25}
Dim a4() As Integer = New Integer() {31, 32, 33, 34, 35}
Dim a5() As Integer = New Integer() {41, 42, 43, 44, 45}
For Each i1 As Integer In a1
For Each i2 As Integer In a2
For Each i3 As Integer In a3
For Each i4 As Integer In a4
For Each i5 As Integer In a5
Console.WriteLine(i5)
If i5 = 42 Then
Exit For
End If
Next
Next
Next
Next
Next
End Sub

...全文
370 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
haisumchen 2019-07-10
  • 打赏
  • 举报
回复
问题出在这里吧?If nextFiller Then
秋天之落叶 2019-07-04
  • 打赏
  • 举报
回复
经验告诉我们,这种情况绝对是循环逻辑写的问题,因为VB不可能出现这样的基本错误,哈哈
轻鸿万里 2019-06-27
  • 打赏
  • 举报
回复
引用 楼主 lmhcs 的回复:
今天调试下程序时,忽然发现,exit for 一下子跳出两层的for。eixt for 不是只跳所在的那一层而已吗?蒙了。见下面代码

'筛选符合条件
For Each s As String In arr 'for1
If String.IsNullOrEmpty(s) Then Continue For
Dim nextFiller As Boolean = False
For Each b As fhBuilding In buildings 'for2
If nextFiller Then
nextFiller = False
Exit For
End If
For Each r As fhRoom In b.Rooms 'for3
If String.Equals(s, r.FullName, StringComparison.OrdinalIgnoreCase) Then
_VisRooms.Add(r)
nextFiller = True
Exit For '为什么exit for 不是离开for3 而是离开for2,直接跳到for1呢?
End If
Next
Next
Next



为了验证exit for 跳跃能力,特写了下面代码,发现exit for 就只能跳出所在的for 层,但为什么上面代码一下子跳出两层呢?


Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim a1() As Integer = New Integer() {1, 2, 3, 4, 5}
Dim a2() As Integer = New Integer() {11, 12, 13, 14, 15}
Dim a3() As Integer = New Integer() {21, 22, 23, 24, 25}
Dim a4() As Integer = New Integer() {31, 32, 33, 34, 35}
Dim a5() As Integer = New Integer() {41, 42, 43, 44, 45}
For Each i1 As Integer In a1
For Each i2 As Integer In a2
For Each i3 As Integer In a3
For Each i4 As Integer In a4
For Each i5 As Integer In a5
Console.WriteLine(i5)
If i5 = 42 Then
Exit For
End If
Next
Next
Next
Next
Next
End Sub





关键是你for2 是判断 nextFiller = True就退出循环,而你for3赋值了 nextFiller = True才退出,所以连for2也退出了。
jhonsonzhang 2019-06-05
  • 打赏
  • 举报
回复
For Each s As String In arr 'for1
If String.IsNullOrEmpty(s) Then Continue For
Dim nextFiller As Boolean = False
For Each b As fhBuilding In buildings 'for2
If nextFiller Then
nextFiller = False
Exit For
End If
dim tpFiller=false
For Each r As fhRoom In b.Rooms 'for3
If String.Equals(s, r.FullName, StringComparison.OrdinalIgnoreCase) Then
_VisRooms.Add(r)
tpFiller = True
Exit For '为什么exit for 不是离开for3 而是离开for2,直接跳到for1呢?
End If
Next
nextfiller=tpfiller
Next
Next

这下就不会exit 到for1 了。但这代码太冗余了,用linq lamada应该2行就解决了。看着还不头疼。
  • 打赏
  • 举报
回复
我看见一个奇怪现象就是邀请回答往往出现404第二天再看又有,但有的也是打不开的网址. 每循环打断点应该可以看到第三层跳出后第二层还应该循环判断一下才跳出,貌似连续跳出2层循环,这完全是设计的逻辑问题.
stherix 2019-05-31
  • 打赏
  • 举报
回复
因为跳出之前 设置了 nextFiller = True 而在for2里面判断nextFiller 为 True会继续跳出
eaqpi 2019-05-31
  • 打赏
  • 举报
回复
nextFiller = True
正怒月神 2019-05-31
  • 打赏
  • 举报
回复
VB不熟悉,但是有没有类似GO的语法呢?
  • 打赏
  • 举报
回复
从第三循环看改变了第二循环的BOOL变量,因此直接跳出2层. 可以逐层调试跟踪,总会找到问题所在.
  • 打赏
  • 举报
回复
跟踪一下查看各变量和条件,或许上层刚好结束了?
良朋 2019-05-31
  • 打赏
  • 举报
回复
5楼详细地给了你答案

16,553

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧