当前位置:网站首页>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
边栏推荐
- 入侵检测领域数据集总结
- 04. 项目博客之日志
- Station B, Mr. Liu Er - multiple logistic regression, structure 7
- Rustdesk builds its own remote desktop relay server
- 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
- What is independent IP and how about independent IP host?
- How to get list length
- Embedded interview questions (IV. common algorithms)
- 华为路由器忘记密码怎么恢复
- Vulhub vulnerability recurrence 67_ Supervisor
猜你喜欢

Report on market depth analysis and future trend prediction of China's arsenic trioxide industry from 2022 to 2028

自建DNS服务器,客户端打开网页慢,解决办法

29io stream, byte output stream continue write line feed

A master in the field of software architecture -- Reading Notes of the beauty of Architecture

What impact will frequent job hopping have on your career?

04. Project blog log

Redis message queue

P2802 go home

B站刘二大人-线性回归及梯度下降

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
随机推荐
【torch】|torch. nn. utils. clip_ grad_ norm_
How can large websites choose better virtual machine service providers?
实践分享:如何安全快速地从 Centos迁移到openEuler
【云原生】3.1 Kubernetes平台安装KubeSpher
What impact will frequent job hopping have on your career?
Station B, Master Liu Er - back propagation
[QNX Hypervisor 2.2用户手册]6.3.3 使用共享内存(shmem)虚拟设备
Mysql database master-slave cluster construction
Codeless June event 2022 codeless Explorer conference will be held soon; AI enhanced codeless tool launched
Anti shake and throttling are easy to understand
[QNX hypervisor 2.2 user manual]6.3.3 using shared memory (shmem) virtual devices
js Array 列表 实战使用总结
数字经济破浪而来 ,LTD是权益独立的Web3.0网站?
H3C S5820V2_5830V2交换机IRF2堆叠后升级方法
SequoiaDB湖仓一体分布式数据库2022.6月刊
(column 22) typical column questions of C language: delete the specified letters in the string.
移植InfoNES到STM32
Closure, decorator
Vulhub vulnerability recurrence 69_ Tiki Wiki
Self built DNS server, the client opens the web page slowly, the solution