当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
Why do job hopping take more than promotion?
Kubernetes learning summary (20) -- what is the relationship between kubernetes and microservices and containers?
面试官:Redis中有序集合的内部实现方式是什么?
Reinforcement learning - learning notes 5 | alphago
Reference frame generation based on deep learning
C language operators
愛可可AI前沿推介(7.6)
Common English vocabulary that every programmer must master (recommended Collection)
MLP (multilayer perceptron neural network) is a multilayer fully connected neural network model.
[asp.net core] set the format of Web API response data -- formatfilter feature
随机推荐
Tips for web development: skillfully use ThreadLocal to avoid layer by layer value transmission
[wechat applet] operation mechanism and update mechanism
js中,字符串和数组互转(一)——字符串转为数组的方法
过程化sql在定义变量上与c语言中的变量定义有什么区别
Regular expression collection
Intel 48 core new Xeon run point exposure: unexpected results against AMD zen3 in 3D cache
审稿人dis整个研究方向已经不仅仅是在审我的稿子了怎么办?
代理和反向代理
Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices
Reflection operation exercise
Is it profitable to host an Olympic Games?
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
【滑动窗口】第九届蓝桥杯省赛B组:日志统计
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
爱可可AI前沿推介(7.6)
[asp.net core] set the format of Web API response data -- formatfilter feature
1500萬員工輕松管理,雲原生數據庫GaussDB讓HR辦公更高效
Common English vocabulary that every programmer must master (recommended Collection)
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,
请问sql group by 语句问题