当前位置:网站首页>continue和break的区别与用法
continue和break的区别与用法
2022-07-06 05:44:00 【bit..】
一般而言,程序进入循环后 在下一次循环测试之间会执行完循环体的所以语句。continue和break 语句可以根据循环体中测试结果来忽略一部分循环内容,甚至结束循环。
continue 语句
三种循环都可以使用continue语句。执行到该语句时,会跳过本次迭代(即循环)的剩余部分,并开始下一轮迭代。如果continue 语句在嵌套循环内,则只会影响包含该语句的内层循环。
语法
C 语言中 continue 语句的语法:
continue;
流程图
实例
#include <stdio.h>
int main ()
{
/* 局部变量定义 */
int a = 10;
/* do 循环执行 */
do
{
if( a == 15)
{
/* 跳过迭代 */
a = a + 1;
continue;
}
printf("a 的值: %d\n", a);
a++;
}while( a < 20 );
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 16
a 的值: 17
a 的值: 18
a 的值: 19
break语句
C 语言中 break 语句有以下两种用法:
当 break 语句出现在一个循环内时,循环会立即终止,且程序流将继续执行紧接着循环的下一条语句。
它可用于终止 switch 语句中的一个 case。
语法
C 语言中 break 语句的语法:
break;
流程图
实例
#include <stdio.h>
int main ()
{
/* 局部变量定义 */
int a = 10;
/* while 循环执行 */
while( a < 20 )
{
printf("a 的值: %d\n", a);
a++;
if( a > 15)
{
/* 使用 break 语句终止循环 */
break;
}
}
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
a 的值: 10
a 的值: 11
a 的值: 12
a 的值: 13
a 的值: 14
a 的值: 15
边栏推荐
- PDK process library installation -csmc
- Sequoiadb Lake warehouse integrated distributed database, June 2022 issue
- Solution of QT TCP packet sticking
- js Array 列表 实战使用总结
- Clear floating mode
- 29io stream, byte output stream continue write line feed
- Problems encountered in installing mysql8 on MAC
- Quantitative description of ANC noise reduction
- Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028
- Selective parameters in MATLAB functions
猜你喜欢
Deep learning -yolov5 introduction to actual combat click data set training
P2802 go home
YYGH-11-定时统计
网络协议模型
华为BFD的配置规范
What preparations should be made for website server migration?
[Jiudu OJ 08] simple search x
Vulhub vulnerability recurrence 71_ Unomi
应用安全系列之三十七:日志注入
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
随机推荐
Selective parameters in MATLAB functions
查詢生產訂單中某個(些)工作中心對應的標准文本碼
Download, install and use NVM of node, and related use of node and NRM
[JVM] [Chapter 17] [garbage collector]
CoDeSys note 2: set coil and reset coil
Note the various data set acquisition methods of jvxetable
C Advanced - data storage (Part 1)
02. Develop data storage of blog project
04. 项目博客之日志
【经验】win11上安装visio
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
PDK工藝庫安裝-CSMC
26file filter anonymous inner class and lambda optimization
A master in the field of software architecture -- Reading Notes of the beauty of Architecture
AUTOSAR从入门到精通番外篇(十)-嵌入式S19文件解析
清除浮动的方式
Vulhub vulnerability recurrence 69_ Tiki Wiki
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
改善Jpopup以实现动态控制disable
js Array 列表 实战使用总结