当前位置:网站首页>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);
}
边栏推荐
- Notes - detailed steps of training, testing and verification of yolo-v4-tiny source code
- Deployment of external server area and dual machine hot standby of firewall Foundation
- 2022菲尔兹奖揭晓!首位韩裔许埈珥上榜,四位80后得奖,乌克兰女数学家成史上唯二获奖女性
- How to turn a multi digit number into a digital list
- Ravendb starts -- document metadata
- Simple continuous viewing PTA
- Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
- 7. Data permission annotation
- [200 opencv routines] 220 Mosaic the image
- R language visualizes the relationship between more than two classification (category) variables, uses mosaic function in VCD package to create mosaic plots, and visualizes the relationship between tw
猜你喜欢

PHP saves session data to MySQL database

Swagger UI教程 API 文档神器

OneNote in-depth evaluation: using resources, plug-ins, templates

SAP Fiori应用索引大全工具和 SAP Fiori Tools 的使用介绍

PHP online examination system version 4.0 source code computer + mobile terminal

Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software

Statistical inference: maximum likelihood estimation, Bayesian estimation and variance deviation decomposition

Common English vocabulary that every programmer must master (recommended Collection)

968 edit distance

20220211 failure - maximum amount of data supported by mongodb
随机推荐
Aike AI frontier promotion (7.6)
How to turn a multi digit number into a digital list
ICML 2022 | Flowformer: 任务通用的线性复杂度Transformer
15 millions d'employés sont faciles à gérer et la base de données native du cloud gaussdb rend le Bureau des RH plus efficace
防火墙基础之外网服务器区部署和双机热备
Reinforcement learning - learning notes 5 | alphago
拼多多败诉,砍价始终差0.9%一案宣判;微信内测同一手机号可注册两个账号功能;2022年度菲尔兹奖公布|极客头条
愛可可AI前沿推介(7.6)
3D人脸重建:从基础知识到识别/重建方法!
Why do job hopping take more than promotion?
HMS core machine learning service creates a new "sound" state of simultaneous interpreting translation, and AI makes international exchanges smoother
039. (2.8) thoughts in the ward
Swagger UI教程 API 文档神器
Is it profitable to host an Olympic Games?
[MySQL] basic use of cursor
全网最全的新型数据库、多维表格平台盘点 Notion、FlowUs、Airtable、SeaTable、维格表 Vika、飞书多维表格、黑帕云、织信 Informat、语雀
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
document.write()的用法-写入文本——修改样式、位置控制
It's almost the new year, and my heart is lazy
OSPF多区域配置