当前位置:网站首页>关键字_02break与continue
关键字_02break与continue
2022-07-24 05:16:00 【乖女子@@@】
break
语法: break [label]
作用1
[1]break; break语句终止当前循环,并把程序控制流转到紧接着被中止语句后面的语句;
举例说明
const arr = [1,3,5,7,8] let sum1 = 0; let sum2 = 0 ; for(let i =0 ;i< arr.length; i++){ sum1+= arr[i] for(let j= 0 ;j< arr.length ;j++){ sum2+=arr[j] if(arr[j]==1){ break } } } console.log('结果',sum1,sum2) // 24,5i第一次循环 -> j=1时 break 结束了j循环,但是i循环依旧执行;
正常情况下break只会退出最近的一个循环!
作用2
[2]break label;break会终止label标签的循环;
举例说明
const arr = [1,3,5,7,8] let sum1 = 0; let sum2 = 0 ; outer:for(let i =0 ;i< arr.length; i++){ sum1+= arr[i] inner:for(let j= 0 ;j< arr.length ;j++){ sum2+=arr[j] if(arr[j]==1){ break outer } } } console.log('结果',sum1,sum2) // 1,1i第一次循环 -> j=1时 break outer结束了外层循环(也就是整个循环)
注意点:一个
break语句必须内嵌在它引用的标记中;举例说明
const arr = [1,3,5,7,8] let sum1 = 0; let sum2 = 0 ; outer:for(let i =0 ;i< arr.length; i++){ sum1+= arr[i] inner:for(let j= 0 ;j< arr.length ;j++){ sum2+=arr[j] } } if(sum1 && sum2 ){ break outer // Undefined label 'outer' }相当于标签只是当前语句体的一个局部变量;
作用3:
break 在switch语句中,当遇到匹配到 case 后,就会执行相应的代码并中断循环体;
const food = "sushi"; switch (food) { case "sushi": console.log("Sushi is originally from Japan."); break; case "pizza": console.log("Pizza is originally from Italy."); break; default: console.log("I have never heard of that dish."); break; }
continue
作用:continue 声明终止当前循环或标记循环的当前迭代中的语句执行,并在下一次迭代时继续执行循环;
举例说明
let text = ''; for (let i = 0; i < 10; i++) { if (i === 3) { continue; } text = text + i; } console.log(text); // '012456789'
边栏推荐
猜你喜欢
随机推荐
Read "effective managers - Drucker"
力扣、牛客网->链表相关题目(篇一)(c语言)
C语言入门篇 概述
Add, delete, modify and check JDBC
Implementation and comparison of nine sorting (ten thousand words summary)
Introduction to threads
谈谈对未来的想法
DNS domain name resolution service
跟李沐学ai 线性回归 从零开始的代码实现超详解
Installation and login login
Generics and annotations
C语言入门篇 一.分支和循环语句
select_渲染小现象
Detailed explanation of string constant pool and intern() of string
C语言进阶篇 二. 指针
Integration of SSM
A collation of the basic usage of numpy
MySQL 远程连接错误解决方法
C文件读写加链表增删改查
反射的介绍






![[deep learning] (III) image classification](/img/8a/86b8ec6951f112f47be816378ab7d8.png)


