当前位置:网站首页>The difference between break and continue
The difference between break and continue
2022-06-21 20:28:00 【[official recommendation] Tangcheng】
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 .
Extended data : stay while and do-while In circulation ,continue Statement causes the process to jump directly to the test part of the loop control condition , And then decide whether the cycle continues . stay for loop in , encounter continue after , Skip the remaining statements in the body of the loop , And go right for Statement “ expression 3” evaluation , Then proceed “ expression 2” Condition test of , According to the “ expression 2” The value of for loop Whether to carry out . Recycle the body , Regardless of continue As a component of what kind of sentence , All will be executed according to the above functions , With this break Somewhat different .
Extended reading :
break: This statement causes the program to terminate the loop that contains it , And proceed to the next stage of the process ( The statement after the whole loop ), namely , Instead of jumping to the next cycle, exit the cycle . If break sentence Contained in nested loops , It just jumps out of the innermost loop .
The following code
public class Test {
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5){
break;
}
System.out.print(i+" ");
}
}
}You can see the program output 01234 To 5 I jumped out of the loop
continue: Loop statement When there is this statement in , When the program runs this statement , Not in execution The loop body in continue Instead, the following statement jumps to the entry of the next loop and executes the next loop .
hold break Change it to continue
public class Test {
public static void main(String[] args) {
for(int i=0; i<10; i++){
if(i==5){
continue;
}
System.out.print(i+" ");
}
}
}You can see that only 5 No output , That is, when i=5 Printing operation was not performed when , Go straight to the next cycle
and return Yes means to return from the called function to the calling function and continue to execute , Return with a return value , from return Later parameter assignment .return Then the function ends , The following statement is no longer executed
public class Test {
public static void fun(){
for(int i=0; i<10; i++){
if(i==5){
return;
}
System.out.print(i+" ");
}
System.out.println("hello");
}
public static void main(String[] args) {
fun();
}
}You can see the back hello It didn't print out
Extended reading :break,continue,return The difference between
1)break Jump right out of the current cycle , Start from outside of the current loop , Ignore any other statements and loop condition tests in the loop body . He can only jump out of a circle , If your loop is nested , Then you need to follow your nesting level , Step by step break To jump out .
2)continue It's also the end of the current cycle , But he doesn't jump out of the loop , But continue to judge the loop condition execution statement . He can only end one cycle , But you can't stop the loop and continue .
3)return Statements can be used to make The branching program returns to the method calling it .( Puzzled )
return Indicates to abort the operation of the current function , And return the operation right to the caller .
If it's in main Function , Means to return the operation right to the operating system .
return It is not necessary to return a value .
void func(void)
{
......
......
return;
}
Of course , Even if you don't write return, After the function is executed, the operation right will also be returned to the caller . Write return It's a clear style , Can prevent some unexpected errors . So the book only says that it should write , Instead of having to write .
If you meet a certain condition and want to quit , It can be used return return , Otherwise, you can not write this code , When the program is executed to “}” Will automatically return to , In fact, you can know this problem by trying it on the machine . But as a good programming habit , It is true that every function has a return statement , This is more readable , And facilitate the maintenance of the program ~~~~
return One instruction is to return a specified data to the calling function ,
Another function is end The execution of the function ...


Five years from programmer to architect ! This is the best programmer career plan I've ever seen
( Next ) 16 strip yyds Code specification for
40 individual SpringBoot Commonly used annotations

Want to follow more dry goods and good articles in real time , Scan the following figure to follow or wechat search 【 Ten thousand words in the book 】 Pay attention to the official account :

At the end :

vx Front and back source code .7z、 Wechat applet front and back source code :vx Front and back source code .7z、 Wechat applet front and back source code
0 Wechat applet for obtaining points 、 Multi type resources .zip:0 Wechat applet for obtaining points 、 Multi type resources .zip
Wechat applet source code - Collection 6 Wechat applet source code - Collection 6,0 Download points _、 Tangcheng -CSDN Blog _vx Applet
Wechat applet source code - Collection 5 Wechat applet source code - Collection 5(0 Download points )_、 Tangcheng -CSDN Blog
Wechat applet source code - Collection 4 https://blog.csdn.net/qq_41570658/article/details/114753075
Wechat applet source code - Collection 3 Wechat applet source code - Collection 3(0 Download points )_、 Tangcheng -CSDN Blog _ E-commerce wechat applet source code
Wechat applet source code - Collection 2 Wechat applet source code - Collection 2(0 Download points )_、 Tangcheng -CSDN Blog
Wechat applet source code - Collection 1 Wechat applet source code - Collection 1(0 Download points )_、 Tangcheng -CSDN Blog
边栏推荐
- 技术实践 | 场景导向的音视频通话体验优化
- Delete the penultimate node - linked list topic
- Source code analysis of ArrayList
- 大魚吃小魚小遊戲完整版
- [complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline
- 金鱼哥RHCA回忆录:DO447Ansible Tower导航
- Summary of methods for NSIS to run bat
- Netcore3.1 Ping whether the network is unblocked and obtaining the CPU and memory utilization of the server
- 贪吃蛇游戏项目完整版
- RestTemplate多个认证信息Authorization问题
猜你喜欢

JVM memory structure

点云转深度图:转化,保存,可视化

汇编语言贪吃蛇、俄罗斯方块双任务设计实现详解(三)——俄罗斯方块详细设计

Novice uses apiccloud visual development to build the mall home page

大鱼吃小鱼小游戏完整版

Category

【微信小程序更改appid失败】微信小程序修改appid一直失败报错tourist appid解决办法
![[icml2022] ctrlformer: learn the transferable state representation of visual control through the transformer](/img/d7/483ba497a72bd73b7ebc004bc922cb.png)
[icml2022] ctrlformer: learn the transferable state representation of visual control through the transformer
![[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline](/img/fe/c204ea78051fb0544b27d44e6d1297.png)
[complete course of time series prediction] take temperature prediction as an example to illustrate the composition of the paper and the construction of pytorch code pipeline

Zabbix6.0+timescaledb+ enterprise wechat alarm
随机推荐
mysql如何实现分组求和
MySQL CentOS installation MySQL L8
MySQL 5.7 compilation and installation
[icml2022] ctrlformer: learn the transferable state representation of visual control through the transformer
【CVPR2022】CMU《多模态机器学习》教程,200+页阐述表示、对齐、推理、迁移、生成与量化六大挑战的多模态学习系统知识
How to query the data in MySQL
Comment MySQL additionne les colonnes
研报精选 | 麦肯锡《中国数字创新未来》趋势解读
大魚吃小魚小遊戲完整版
理财产品到期当日能赎回吗?
Zabbix6.0+timescaledb+ enterprise wechat alarm
Big fish eat small fish games full version
Datagear uses coordinate mapping table to make geographic coordinate data visualization Kanban
1156 Sexy Primes
Inno setup change installation path learning
Redis 做缓存场景引发的问题
深度学习图像数据增强
细节、MYSQL_DATE_FORMAT()_函数_详解(记得收藏)
SD6.20集训总结
Daily development of common tools to improve efficiency



