当前位置:网站首页>The difference between using switch in a loop and using break and continue after executing a condition
The difference between using switch in a loop and using break and continue after executing a condition
2022-07-28 17:38:00 【Mr_ Bobcp】
Catalog
The test code in this article uses JavaScript Language writing
break and continue Usage list
1.break
use break Statement can make the process jump out of switch Statement of body , It can also be used. break Statement terminates the loop body in the loop structure , So as to end the cycle of this layer ahead of time .
Instructions :
(1) Only in circulation and switch In the body of a sentence break;
(2) When break In the circulatory body switch In the body of a sentence , It works just to get out of the way switch Statement of body , Does not terminate the execution of the loop body . If you want to force the execution of the loop body , It can be in the loop body , But it's not switch Set... In the statement break sentence , If certain conditions are satisfied, you will jump out of the loop .
2.continue
continue The function of the statement is to skip the remaining unexecuted statements in the body of this loop , Immediately carry out the next cycle condition determination , It can be understood as only ending this cycle .
Be careful :continue Statement does not terminate the entire loop .
Use break give an example
From the print results, it is not difficult to see the use break, When the condition is met, in addition to executing switch sentence , It will also continue to execute the remaining unexecuted statements in the body of this loop
let fruit=["apple", "banana", "orange", "pear"]
for (let i = 0; i < fruit.length; i++) {
switch (fruit[i]) {
case "orange":
console.log(" Hey, hey, hey ");
break;
// continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
/* Print the results we have apple we have banana Hey, hey, hey we have orange we have pear */
Use continue give an example
It can be seen from the print results that switch Use after condition continue When , When the execution meets the condition switch After the statement , The remaining unexecuted statements in the loop body will be skipped , Execute next cycle
let fruit=["apple", "banana", "orange", "pear"]
for (let i = 0; i < fruit.length; i++) {
switch (fruit[i]) {
case "orange":
console.log(" Hey, hey, hey ");
// break;
continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
/* Print the results we have apple we have banana Hey, hey, hey we have pear */
extend , Used in multi-level nested loops continue
It can be seen from the above two single-layer cycle examples switch A condition of break It will not have a skip effect on the remaining statements in this cycle , and continue Meeting , So many cycles are in the deep cycle switch Use continue Will it affect the circulation of the outer layer , The answer is no ,
continue Only right “ The innermost layer ” Of “ This cycle ” take effect , Skip the rest of the loop body in this loop . For the next cycle 、 Or the outer circle , It won't work .
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(" Hey, hey, hey ");
// break;
continue;
default:
break;
}
console.log("we have " + fruit[i]);
}
}
/* Print the results Four times the same : we have apple we have banana Hey, hey, hey we have pear */
边栏推荐
- JS implementation of special prime numbers
- [CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
- 【无标题】
- 医学公共数据库
- LNMP源码编译安装
- C # basic interview questions (with answers)
- 2021 年全国大学生数据统计与分析竞赛
- Mqtt.fx connects to Alibaba cloud Internet of things platform
- 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
- FreeRTOS learning notes
猜你喜欢
![[kibana] problem sorting kibana 7.x no indices match pattern](/img/36/0f85dbe7eea49db8f845a4a4f302cf.png)
[kibana] problem sorting kibana 7.x no indices match pattern "APM-*“

Blue Bridge Cup embedded competition resources and skills

Verilog 每日一题(VL4 移位运算与乘法)

【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息

部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装
![[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

Linear algebra and matrix theory (7)

【atlas】atlas 编译报错整理(全)

MySQL triggers

Verilog daily question (vl6 data series to parallel circuit)
随机推荐
Verilog daily question (vl8 uses generate... For statement to simplify code)
R语言画图/绘图/作图2
Batch download files
渗透测试大杀器kali安装配置
Verilog daily question (vl24 multi bit MUX synchronizer cross time domain output)
LNMP source code compilation and installation
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
阿里P8架构师谈:成为架构师必须学好的七大知识点(含面试题)
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
LNMP源码编译安装
Adding new objects to the object array in JS results in the modification of existing objects in the array
Verilog daily question (vl14 vending machine 1 -- FSM common question types)
在PDF中插入文本水印
Easy to use vscode plug-in memo
部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装
[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
Master the key points of JVM interview
【ansible】ansible shell 执行远程命令遇到awk 中$符号的问题
漫谈测试平台—平台建设思路(上)
【kibana】问题整理 kibana 7.x No indices match pattern “apm-*“