当前位置:网站首页>The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
The difference between break and continue in the for loop -- break completely end the loop & continue terminate this loop
2022-07-06 21:10:00 【viceen】
for In circulation break And continue The difference between ——break- Complete end of cycle & continue- Terminate the loop
stay for In circulation break And continue The difference is as follows :
break Used to completely end a cycle , Jump out of the loop body and execute the statement after the loop ; and continue Is to skip the remaining statements in the current loop , Execute next cycle . In short, it's break Complete end of cycle ,continue Terminate the loop .
1、continue- Terminate the loop
for (let i = 1; i < 5; i++) {
if (i === 2) {
continue;
}
console.log(i) // 1 3 4
}
2、break- Complete end of cycle
for (let i = 1; i < 5; i++) {
if (i === 2) {
break;
}
console.log(i) // 1
}
example
var methodInfoList = [
{
value:' Xiao Ming ',id:3},
{
value:' Xiaohong ',id:4},
{
value:' cockroach ',id:2},
]
var sign
for(var i=0, len = methodInfoList.length ; i< len ; i++){
if(methodInfoList[i].value == ' Xiaohong ') {
sign = 3
console.log(798);
break;
}
console.log(123,sign);
if(methodInfoList[i].value == ' cockroach ') {
console.log(852,sign);
break;
}
}
Print display order
123 undefined
798
3、 Comparison of different cycles
js in for Loops can be implemented in many ways , among forEach The way is incompatible break The grammatical .
3.1、 Use traditional for loop
This way supports continue, Also support break grammar
for(var i=0, len = methodInfoList.length ; i< len ; i++){
if(methodInfoList[i].value == null || methodInfoList[i].value == "") {
this.msgError(" The check method cannot enter a null value ");
break;
}
}
3.2、 Use for-in loop
adopt return true Implementation is not supported in this way continue and break The same function is to exit the current cycle ;
adopt return false Realization and break The same function is to exit the whole cycle
$.each(arr,function(index,oo){
if(index == 2){
return true;
}
if(index == 5){
return false;
}
})
3.3、 Use forEach loop
This way does not support continue and break, Nor does it support return The way ;
If you need to jump out of the loop, you can only throw exceptions
try {
methodInfoList.forEach(element => {
if (element.value == null || element.value == "") {
this.msgError(" The check method cannot enter a null value ");
throw new Error(" The check method cannot enter a null value ");
}
});
} catch(e){
console.log(e.message);
}
边栏推荐
- No Yum source to install SPuG monitoring
- 039. (2.8) thoughts in the ward
- Distributed ID
- None of the strongest kings in the monitoring industry!
- C language games - minesweeping
- 1_ Introduction to go language
- OneNote 深度评测:使用资源、插件、模版
- What's the best way to get TFS to output each project to its own directory?
- SAP UI5 框架的 manifest.json
- Spark SQL chasing Wife Series (initial understanding)
猜你喜欢
##无yum源安装spug监控
[MySQL] basic use of cursor
Distributed ID
基于深度学习的参考帧生成
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,
20220211 failure - maximum amount of data supported by mongodb
PHP online examination system version 4.0 source code computer + mobile terminal
1500万员工轻松管理,云原生数据库GaussDB让HR办公更高效
Performance test process and plan
SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍
随机推荐
PG基础篇--逻辑结构管理(事务)
Select data Column subset in table R [duplicate] - select subset of columns in data table R [duplicate]
KDD 2022 | 通过知识增强的提示学习实现统一的对话式推荐
监控界的最强王者,没有之一!
js中,字符串和数组互转(一)——字符串转为数组的方法
New database, multidimensional table platform inventory note, flowus, airtable, seatable, Vig table Vika, Feishu multidimensional table, heipayun, Zhixin information, YuQue
防火墙基础之外网服务器区部署和双机热备
Pat 1085 perfect sequence (25 points) perfect sequence
Nodejs tutorial let's create your first expressjs application with typescript
【论文解读】用于白内障分级/分类的机器学习技术
Laravel notes - add the function of locking accounts after 5 login failures in user-defined login (improve system security)
Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
Is this the feeling of being spoiled by bytes?
Swagger UI tutorial API document artifact
038. (2.7) less anxiety
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
Aike AI frontier promotion (7.6)
Regular expression collection
No Yum source to install SPuG monitoring
966 minimum path sum