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

...全文
500 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楼详细地给了你答案
代码下载链接: https://pan.quark.cn/s/a8fbca3925b4 《鸿蒙OS开发环境构建》指南系统性地阐述了配置和筹备鸿蒙OS开发所需的各种工具和条件的具体方法。鸿蒙OS,亦称HarmonyOS,是由华为研发的一款面向全场景的分布式操作系统,其目标是提供跨平台、多设备间无缝协作的使用体验。本指南涉及了从Linux服务器到Windows工作站的完整开发流程。指南中提及了MobaXterm,这是一款用于连接Linux源码服务器的软件,使得开发人员能够在Windows环境中远程访问Linux服务器。同时,HiTool作为烧录工具,用于将编译后的系统镜像写入开发板。IPOP.EXE则是一款串口终端软件,用于执行串行通信和调试任务。Embedded Studio用于开发设备驱动程序,而DevEco Studio是华为提供的图形化应用程序开发平台,支持C/C++语言,拥有代码编辑、编译、烧录和调试功能,被视为OpenHarmony智能设备开发者的首选集成开发环境。在硬件配置方面,指南列出了必需的设备,包括Linux服务器(推荐Ubuntu 16.04及以上版本),Windows工作台(兼容XP/7/10),以及Hi3518EV300 IoT Camera单板。开发期间,Windows工作台通过USB线与单板相连接,以实现数据传输。此外,为了开展开发工作,还需要安装putty、IPOP、tftp服务器等辅助软件,以及HiTool用于烧录操作。在软件系统要求方面,Linux服务器需要安装bash、Python3.7+、gn、ninja、LLVM等构建工具,这些工具对于生成和执行编译脚本具有关键作用。在Windows工作台上,建议采用Visual Studi...

16,718

社区成员

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

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