当前位置:网站首页>循环中使用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 */
边栏推荐
- Shell脚本之AWK
- 【presto】presto 常用的命令
- Verilog daily question (vl5 signal generator)
- LNMP源码编译安装
- Solve the problem of exclusive SQL Server database
- [atlas] atlas compilation error sorting (all)
- GEAR: Graph-based Evidence Aggregating and Reasoning for Fact Verification
- Verilog daily question (vl4 shift operation and multiplication)
- Codeworks round 801 (Div. 2) and epic Institute of technology round D. tree queries (tree DP)
- Encrypt the video and upload it to OSS to achieve high concurrent access
猜你喜欢

Basic principle of asynchronous FIFO (simple implementation based on Verilog)

Firewalld防护墙

异步电路设计--同步脉冲器原理及例题

With a total data volume of more than trillions of lines, Yuxi cigarette factory can easily deal with it by correctly selecting the timing database

利用SQL Server代理作业对数据库进行定时还原

Mysql database addition, deletion, modification and query (detailed explanation of basic operation commands)

一文掌握 JVM 面试要点

SNAT、DNAT 防火墙规则的备份和还原

High speed circuit design practice -- Overview

在android开发过程中遇到.sqlite文件处理
随机推荐
Connection design and test platform -- Summary of SystemVerilog interface knowledge points
Valarray Library Learning
Basic principle of asynchronous FIFO (simple implementation based on Verilog)
wpf命令按钮透明样式
Linear algebra and matrix theory (IX)
AMQP协议详解
解决SQL Server数据库独占的问题
MySQL detailed learning tutorial (recommended Collection)
一文掌握 JVM 面试要点
LNMP源码编译安装
Export word according to the template, generate compound format tables and variable column tables
Goweb开发之Beego框架实战:第四节 数据库配置及连接
堡垒机的作用
Batch download files
Analysis of kubernetes service principle
批量下载文件
@RequestParam使用
Encountered.Sqlite file processing during Android Development
Role of Fortress machine
Selection of resistance in high speed circuit