当前位置:网站首页>forEach 中 return 和 for 中 break
forEach 中 return 和 for 中 break
2022-06-11 07:43:00 【叮当的猫猫】
区别

想要终止for循环,使用return

只有赵六没被打印出来,后续的数组元素依旧被执行了。
foreach 中
关键字"return",执行的效果相当于普通for循环里的关键词continue
return的意思也不是原来return代表的含义
"break"或"continue"是不被允许使用的
普通for循环中
想要提前结束(终止)循环体使用"break";
结束本轮循环,进行下一轮循环使用"continue";
使用"return",不仅强制结束for循环体,还会提前结束包含这个循环体的整个方法
原理

forEach(),说到底是一个方法,而不是循环体,结束一个方法的执行 是return
常见操作
for 循环中 break 终止当前循环,继续执行接下来的代码

for 循环中 return 终止当前循环,接下来的代码不执行,方法返回

实际运行中,往往有很多不突发情况导致代码提前终止
比如:空指针异常,所以可以通过抛出假异常的方式来终止forEach

这种方式不友好,可以再包装一层
边栏推荐
- 【AtCoder1981】Shorten Diameter(图论思维)
- VIM common commands
- 【AtCoder2387】+/- Rectangle
- 模线性方程组(中国剩余定理+通用解法)
- Post-processing of ffmpeg miscellaneous notes
- 2022 low voltage electrician test questions and online simulation test
- String Simulation Implementation
- [software testing] 90% of the interviewers have been brushed out of such resumes
- Pat class A by category
- The maximum number of divisors of numbers in the int range is 1536
猜你喜欢

【Oracle 数据库】奶妈式教程day04 排序查询

2021-11-05 definition of cache

Summary of classic interview questions

Software testing weekly (issue 75): only when you look down, can you see your true self.

Flask页面的分页
![2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]](/img/4d/a67a63d2c4eb80c98315c3057b01b9.jpg)
2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]

Black Qunhui dsm7.0.1 physical machine installation tutorial

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

Paging of the flask page

Import on CSDN MD file
随机推荐
Aiop introduction
Matrix tree theorem
[atcoder2376] black and white tree (game)
【AtCoder1984】Wide Swap (拓扑排序转化)
Classes and objects (Part 2)
[atcoder1984] wide swap
2021-11-05 definition of cache
A case in which the MySQL administrator password cannot take effect
Sort - merge sort
Wc2020 course selection
JVM tuning
Compound RateModel合约解析
Zero foundation self-study SQL course | outer join external connection
C language judging big end and small end [consortium or pointer] big end and small end conversion
Arduino_ Esp32 development record
Post-processing of ffmpeg miscellaneous notes
【AtCoder2000】Leftmost Ball (DP+组合数)
【HDU6357】Hills And Valleys(DP)
Pat class A by category
[Oracle database] mammy tutorial day03 Sorting Query