当前位置:网站首页>for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
2022-07-06 12:51:00 【viceen】
for循环中break与continue的区别——break-完全结束循环 & continue-终止本次循环
在for循环中break与continue的区别如下:
break用于完全结束一个循环,跳出循环体执行循环后面的语句;而continue是跳过当次循环中剩下的语句,执行下一次循环。简单点说就是break完全结束循环,continue终止本次循环。

1、continue-终止本次循环
for (let i = 1; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i) // 1 3 4
}
2、break-完全结束循环
for (let i = 1; i < 5; i++) {
if (i === 2) {
break;
}
console.log(i) // 1
}
实例
var methodInfoList = [
{
value:'小明',id:3},
{
value:'小红',id:4},
{
value:'小强',id:2},
]
var sign
for(var i=0, len = methodInfoList.length ; i< len ; i++){
if(methodInfoList[i].value == '小红') {
sign = 3
console.log(798);
break;
}
console.log(123,sign);
if(methodInfoList[i].value == '小强') {
console.log(852,sign);
break;
}
}
打印显示顺序
123 undefined
798
3、不同循环的比较
js中for循环有多种实现方式,其中forEach的方式是不兼容break语法的。
3.1、使用传统的for循环
这种方式支持continue,也支持break语法
for(var i=0, len = methodInfoList.length ; i< len ; i++){
if(methodInfoList[i].value == null || methodInfoList[i].value == "") {
this.msgError("检查方法不能输入空值");
break;
}
}
3.2、使用for-in循环
通过return true实现与这种方式不支持continue和break相同的功能即退出当前循环;
通过 return false实现与break相同的功能即退出整个循环
$.each(arr,function(index,oo){
if(index == 2){
return true;
}
if(index == 5){
return false;
}
})
3.3、使用forEach循环
这种方式不支持continue和break,也不支持return的方式;
如果需要跳出循环只能通过抛异常的方式实现
try {
methodInfoList.forEach(element => {
if (element.value == null || element.value == "") {
this.msgError("检查方法不能输入空值");
throw new Error("检查方法不能输入空值");
}
});
} catch(e){
console.log(e.message);
}
边栏推荐
- @PathVariable
- Activiti global process monitors activitieventlistener to monitor different types of events, which is very convenient without configuring task monitoring in acitivit
- 强化学习-学习笔记5 | AlphaGo
- Distributed ID
- APS taps home appliance industry into new growth points
- Taylor series fast Fourier transform (FFT)
- KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
- What key progress has been made in deep learning in 2021?
- The mail command is used in combination with the pipeline command statement
- How to upgrade high value-added links in the textile and clothing industry? APS to help
猜你喜欢
![[MySQL] basic use of cursor](/img/cc/39b1e17b48d0de641d3cbffbf2335a.png)
[MySQL] basic use of cursor

OneNote 深度评测:使用资源、插件、模版

性能测试过程和计划
![[wechat applet] operation mechanism and update mechanism](/img/cf/58a62a7134ff5e9f8d2f91aa24c7ac.png)
[wechat applet] operation mechanism and update mechanism

Reviewer dis's whole research direction is not just reviewing my manuscript. What should I do?

What key progress has been made in deep learning in 2021?

【微信小程序】運行機制和更新機制

2017 8th Blue Bridge Cup group a provincial tournament

3D人脸重建:从基础知识到识别/重建方法!

【mysql】触发器
随机推荐
How to turn a multi digit number into a digital list
Deployment of external server area and dual machine hot standby of firewall Foundation
请问sql group by 语句问题
[MySQL] basic use of cursor
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
Common English vocabulary that every programmer must master (recommended Collection)
Implementation of packaging video into MP4 format and storing it in TF Card
Spiral square PTA
C # use Oracle stored procedure to obtain result set instance
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
3D face reconstruction: from basic knowledge to recognition / reconstruction methods!
动态切换数据源
2022 Guangdong Provincial Safety Officer C certificate third batch (full-time safety production management personnel) simulation examination and Guangdong Provincial Safety Officer C certificate third
华为设备命令
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Mécanisme de fonctionnement et de mise à jour de [Widget Wechat]
【深度学习】PyTorch 1.12发布,正式支持苹果M1芯片GPU加速,修复众多Bug
全网最全的知识库管理工具综合评测和推荐:FlowUs、Baklib、简道云、ONES Wiki 、PingCode、Seed、MeBox、亿方云、智米云、搜阅云、天翎