当前位置:网站首页>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);
}
边栏推荐
- [200 opencv routines] 220 Mosaic the image
- 2017 8th Blue Bridge Cup group a provincial tournament
- Forward maximum matching method
- Can novices speculate in stocks for 200 yuan? Is the securities account given by qiniu safe?
- 2022 fields Award Announced! The first Korean Xu Long'er was on the list, and four post-80s women won the prize. Ukrainian female mathematicians became the only two women to win the prize in history
- 过程化sql在定义变量上与c语言中的变量定义有什么区别
- OAI 5g nr+usrp b210 installation and construction
- None of the strongest kings in the monitoring industry!
- 愛可可AI前沿推介(7.6)
- [MySQL] trigger
猜你喜欢
Data Lake (VIII): Iceberg data storage format
SAP UI5 框架的 manifest.json
No Yum source to install SPuG monitoring
OneNote 深度评测:使用资源、插件、模版
[200 opencv routines] 220 Mosaic the image
【OpenCV 例程200篇】220.对图像进行马赛克处理
2017 8th Blue Bridge Cup group a provincial tournament
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
After working for 5 years, this experience is left when you reach P7. You have helped your friends get 10 offers
基于深度学习的参考帧生成
随机推荐
C language games - three chess
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
嵌入式开发的7大原罪
Nodejs教程之让我们用 typescript 创建你的第一个 expressjs 应用程序
Pat 1085 perfect sequence (25 points) perfect sequence
Mtcnn face detection
[MySQL] basic use of cursor
Study notes of grain Mall - phase I: Project Introduction
js中,字符串和数组互转(一)——字符串转为数组的方法
[200 opencv routines] 220 Mosaic the image
Taylor series fast Fourier transform (FFT)
快过年了,心也懒了
【mysql】游标的基本使用
SAP UI5 框架的 manifest.json
Yyds dry inventory run kubeedge official example_ Counter demo counter
如何实现常见框架
【微信小程序】運行機制和更新機制
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,
爱可可AI前沿推介(7.6)
What is the problem with the SQL group by statement