当前位置:网站首页>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 */
边栏推荐
- Verilog daily question (vl29 single port RAM)
- Talk about what you know about publishing online (2)
- Selection and application of capacitor in high speed circuit -- detailed explanation
- [CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
- Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
- Redis源码剖析,狠狠地拿捏了,赶紧码住
- Convert multidimensional object array to one-dimensional array
- Flat data to tree
- 一文掌握 JVM 面试要点
- [impala] [error reporting solution] impala cannot read or execute the parent directory of dfs.domain.socket.path
猜你喜欢

Encrypt the video and upload it to OSS to achieve high concurrent access
![[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

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

【无标题】

R中因子(factor)

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

漫谈测试平台—平台建设思路(上)

Ant financial mobile testing tool solopi monitoring part of the source code guide.. Continuous update

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

面试官:算法刷题实录.pdf我居然答不上来
随机推荐
渗透测试大杀器kali安装配置
Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP
[kibana] problem sorting kibana 7.x no indices match pattern "APM-*“
Linear algebra and matrix theory (VIII)
一文掌握 JVM 面试要点
Punctual atomic serial port protocol
Adding new objects to the object array in JS results in the modification of existing objects in the array
JS中为对象数组添加新对象结果导致数组中已存在的对象也修改了
Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
Selection of resistance in high speed circuit
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
SNAT、DNAT 防火墙规则的备份和还原
[Presto] common commands of Presto
Wild pointer recurrence
Verilog daily question (simple implementation of VL30 RAM)
Insert text watermark in PDF
Application system log structure of elastic stack
【sqoop】sqoop1.4.7 安装集成CDH5.13
PCA 报错Error in eigen(crossprod(t(X), t(X)), symmetric = TRUE) : ‘x‘里有无穷值或遗漏值
MySQL implements sorting according to custom (specified order)