当前位置:网站首页>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);
}
边栏推荐
- 知识图谱构建流程步骤详解
- [diy] how to make a personalized radio
- 每个程序员必须掌握的常用英语词汇(建议收藏)
- Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
- R語言可視化兩個以上的分類(類別)變量之間的關系、使用vcd包中的Mosaic函數創建馬賽克圖( Mosaic plots)、分別可視化兩個、三個、四個分類變量的關系的馬賽克圖
- 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
- PHP online examination system version 4.0 source code computer + mobile terminal
- Value of APS application in food industry
- 硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
- c#使用oracle存储过程获取结果集实例
猜你喜欢
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
使用.Net驱动Jetson Nano的OLED显示屏
面试官:Redis中有序集合的内部实现方式是什么?
Manifest of SAP ui5 framework json
1_ Introduction to go language
Pinduoduo lost the lawsuit, and the case of bargain price difference of 0.9% was sentenced; Wechat internal test, the same mobile phone number can register two account functions; 2022 fields Awards an
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
Entity alignment two of knowledge map
Core principles of video games
No Yum source to install SPuG monitoring
随机推荐
Comprehensive evaluation and recommendation of the most comprehensive knowledge base management tools in the whole network: flowus, baklib, jiandaoyun, ones wiki, pingcode, seed, mebox, Yifang cloud,
2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
过程化sql在定义变量上与c语言中的变量定义有什么区别
Le langage r visualise les relations entre plus de deux variables de classification (catégories), crée des plots Mosaiques en utilisant la fonction Mosaic dans le paquet VCD, et visualise les relation
【微信小程序】运行机制和更新机制
What is the problem with the SQL group by statement
The most comprehensive new database in the whole network, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, flying Book Multidimensional table, heipayun, Zhix
2022 construction electrician (special type of construction work) free test questions and construction electrician (special type of construction work) certificate examination
What key progress has been made in deep learning in 2021?
C language games - three chess
OSPF多区域配置
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
基于STM32单片机设计的红外测温仪(带人脸检测)
防火墙基础之外网服务器区部署和双机热备
Manifest of SAP ui5 framework json
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
Entity alignment two of knowledge map
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
c#使用oracle存储过程获取结果集实例