当前位置:网站首页>循环中使用switch在执行条件后使用break和continue的区别
循环中使用switch在执行条件后使用break和continue的区别
2022-07-28 16:26:00 【Mr_Bobcp】
本文中测试代码使用JavaScript语言编写
break和continue用法一览
1.break
用break语句可以使流程跳出switch语句体,也可以用break语句在循环结构终止本层循环体,从而提前结束本层循环。
使用说明:
(1)只能在循环体内和switch语句体内使用break;
(2)当break出现在循环体中的switch语句体内时,起作用只是跳出该switch语句体,并不能终止循环体的执行。若想强行终止循环体的执行,可以在循环体中,但并不在switch语句中设置break语句,满足某种条件则跳出本层循环体。
2.continue
continue语句的作用是跳过本次循环体中余下尚未执行的语句,立即进行下一次的循环条件判定,可以理解为仅结束本次循环。
注意:continue语句并没有使整个循环终止。
使用break举例
从打印结果不难看出使用break,当满足条件时除了执行满足该条件的switch语句,还会继续执行本次循环体中余下尚未执行的语句
let fruit=["apple", "banana", "orange", "pear"]
for (let i = 0; i < fruit.length; i++) {
switch (fruit[i]) {
case "orange":
console.log("嘿嘿嘿");
break;
// continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
/* 打印结果 we have apple we have banana 嘿嘿嘿 we have orange we have pear */
使用continue举例
从打印结果可以看出在使用switch条件后使用continue的时候,当执行完满足该条件的switch语句后,会跳过本次循环体中余下尚未执行的语句,执行下一个循环
let fruit=["apple", "banana", "orange", "pear"]
for (let i = 0; i < fruit.length; i++) {
switch (fruit[i]) {
case "orange":
console.log("嘿嘿嘿");
// break;
continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
/* 打印结果 we have apple we have banana 嘿嘿嘿 we have pear */
延伸,多层嵌套循环中使用continue
从上文两个单层循环例子可以看出switch的某条件break不会对本次循环剩余语句产生跳过的影响,而continue会,那么多循环在深层循环中的switch使用continue会不会影响外层的循环呢,答案是不会,
continue只对“最内层”的“本次循环”生效,跳过本次循环中循环体的后余部分。对下一轮的循环、或是外层的循环,都不会起作用的。
let fruit=["apple", "banana", "orange", "pear"]
for (let j = 0; j < fruit.length; j++) {
for (let i = 0; i < fruit.length; i++) {
switch (fruit[i]) {
case "orange":
console.log("嘿嘿嘿");
// break;
continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
}
/* 打印结果 四次一样的: we have apple we have banana 嘿嘿嘿 we have pear */
边栏推荐
- Azure Devops developed by visual studio 2015 team
- influxdb2的使用
- Encountered.Sqlite file processing during Android Development
- How do we do full link grayscale on the database?
- Create a custom paging control
- Verilog 每日一题(VL29 单端口RAM)
- Verilog daily question (simple implementation of VL30 RAM)
- Verilog daily question (vl14 vending machine 1 -- FSM common question types)
- Visual studio 2012/2015 releases web applications together with.Cs source code
- 格雷码和二进制的转换及典型例题(4bits格雷码计数器)
猜你喜欢

Goweb开发之Beego框架实战:第四节 数据库配置及连接

Export word according to the template, generate compound format tables and variable column tables

C# 导入Excel文件数据的几种方法
![[impala] [error reporting solution] impala cannot read or execute the parent directory of dfs.domain.socket.path](/img/8f/eb83ff3d5364a704957bc3217ccc2e.png)
[impala] [error reporting solution] impala cannot read or execute the parent directory of dfs.domain.socket.path

连接设计与测试平台——SystemVerilog 接口知识点总结

MySQL详细学习教程(建议收藏)

What does the service grid that has been popular for two years bring to microservices? (Reprinted)

MySQL的触发器

Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)

Goweb开发之Beego框架实战:第二节 项目初始化配置
随机推荐
Kubernetes service and ingress you need to master
线性代数及矩阵论(八)
Goweb开发之Beego框架实战:第四节 数据库配置及连接
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
Verilog daily question (vl24 multi bit MUX synchronizer cross time domain output)
Verilog 每日一题(VL29 单端口RAM)
2021年4月份自考
Verilog daily question (vl5 signal generator)
Problem solution of code heartstrings Junior Group (official competition) of Dalian University of Technology (Development Zone campus) in 2021
[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
The practice of beego framework in goweb development: Section I Introduction to beego framework
Basic principle of asynchronous FIFO (simple implementation based on Verilog)
高速电路设计实践——概述
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
Janus series article 3 API usage guide videoroom creating a new video room
WPF command button transparent style
Andthen of function interface
[atlas] atlas compilation error sorting (all)
Reasoning Over Semantic-Level Graph for Fact Checking
【sqoop】sqoop1.4.7 安装集成CDH5.13