当前位置:网站首页>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
边栏推荐
- Vulhub vulnerability recurrence 72_ uWSGI
- 59. Spiral matrix
- Auto. JS learning notes 17: basic listening events and UI simple click event operations
- Redis message queue
- Pytorch代码注意的细节,容易敲错的地方
- 自建DNS服务器,客户端打开网页慢,解决办法
- Yygh-11-timing statistics
- Web Security (VI) the use of session and the difference between session and cookie
- What preparations should be made for website server migration?
- JS array list actual use summary
猜你喜欢

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

Mysql database master-slave cluster construction

P2802 回家

CoDeSys note 2: set coil and reset coil
![[Jiudu OJ 07] folding basket](/img/a7/e394f32cf7f02468988addad67674b.jpg)
[Jiudu OJ 07] folding basket

华为BFD的配置规范

什么是独立IP,独立IP主机怎么样?

Station B, Master Liu Er - back propagation

H3C防火墙RBM+VRRP 组网配置

Clear floating mode
随机推荐
29io stream, byte output stream continue write line feed
[JVM] [Chapter 17] [garbage collector]
华为路由器如何配置静态路由
Note the various data set acquisition methods of jvxetable
自建DNS服务器,客户端打开网页慢,解决办法
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
B站刘二大人-线性回归及梯度下降
Vulhub vulnerability recurrence 67_ Supervisor
Station B Liu Erden - linear regression and gradient descent
Summary of deep learning tuning tricks
Li Chuang EDA learning notes 12: common PCB board layout constraint principles
清除浮动的方式
Embedded interview questions (IV. common algorithms)
什么是独立IP,独立IP主机怎么样?
无代码六月大事件|2022无代码探索者大会即将召开;AI增强型无代码工具推出...
Station B, Master Liu Er - dataset and data loading
Clear floating mode
Vulhub vulnerability recurrence 71_ Unomi
Sword finger offer II 039 Maximum rectangular area of histogram
First knowledge database