当前位置:网站首页>009 C语言基础:C循环
009 C语言基础:C循环
2022-06-27 04:04:00 【入狱计划进度50%】
一:概述
有的时候,可能需要多次执行同一块代码。一般情况下,语句是顺序执行的:函数中的第一个语句先执行,接着是第二个语句,依此类推。
二:while循环
当给定条件为真时,重复语句或语句组。它会在执行循环主体之前测试条件。
while(){
}
实例:
#include <stdio.h>
int main(){
int a = 10;
while(a<20){
printf("a is : %d \n", a);
a++;
}
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# ./while
a is : 10
a is : 11
a is : 12
a is : 13
a is : 14
a is : 15
a is : 16
a is : 17
a is : 18
a is : 19
三:for循环
多次执行一个语句序列,简化管理循环变量的代码。
for ( init; condition; increment )
{
statement(s);
}
下面是 for 循环的控制流:
init 会首先被执行,且只会执行一次。这一步允许您声明并初始化任何循环控制变量。您也可以不在这里写任何语句,只要有一个分号出现即可。
接下来,会判断 condition。如果为真,则执行循环主体。如果为假,则不执行循环主体,且控制流会跳转到紧接着 for 循环的下一条语句。
在执行完 for 循环主体后,控制流会跳回上面的 increment 语句。该语句允许您更新循环控制变量。该语句可以留空,只要在条件后有一个分号出现即可。
条件再次被判断。如果为真,则执行循环,这个过程会不断重复(循环主体,然后增加步值,再然后重新判断条件)。在条件变为假时,for 循环终止。
实例:
#include <stdio.h>
int main(){
for(int a = 10; a < 20; a = a+1){
printf("a is : %d \n", a);
}
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim for.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc for.c -o for
┌──(rootkali)-[~/Desktop/c_test]
└─# ./for
a is : 10
a is : 11
a is : 12
a is : 13
a is : 14
a is : 15
a is : 16
a is : 17
a is : 18
a is : 19
四:do…while循环
不像 for 和 while 循环,它们是在循环头部测试循环条件。在 C 语言中,do…while 循环是在循环的尾部检查它的条件。do…while 循环与 while 循环类似,但是 do…while 循环会确保至少执行一次循环。
do
{
statement(s);
}while( condition );
实例:
#include <stdio.h>
int main(){
int a = 10;
do{
printf("a is : %d \n", a);
a = a + 1;
}while(a < 20);
return 0;
}
结果:
┌──(rootkali)-[~/Desktop/c_test]
└─# vim dowhile.c
┌──(rootkali)-[~/Desktop/c_test]
└─# gcc dowhile.c -o dowhile
┌──(rootkali)-[~/Desktop/c_test]
└─# ./dowhile
a is : 10
a is : 11
a is : 12
a is : 13
a is : 14
a is : 15
a is : 16
a is : 17
a is : 18
a is : 19
五:嵌套循环
可以在 while、for 或 do…while 循环内使用一个或多个循环。
语法:
C 语言中 嵌套 for 循环 语句的语法:
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
C 语言中 嵌套 while 循环 语句的语法:
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
C 语言中 嵌套 do...while 循环 语句的语法:
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
六:无限循环
#include <stdio.h>
int main ()
{
for( ; ; )
{
printf("hello cccccccc \n");
}
return 0;
}
当条件表达式不存在时,它被假设为真。您也可以设置一个初始值和增量表达式,但是一般情况下,C 程序员偏向于使用 for(;;) 结构来表示一个无限循环。
注意:您可以按 Ctrl + C 键终止一个无限循环。
七:循环控制语句
控制语句 描述
break 语句 终止 loop 或 switch 语句,程序流将继续执行紧接着 loop 或 switch 的下一条语句。
break;
continue 语句 引起循环跳过主体的剩余部分,立即重新开始测试条件。
continue;
goto 语句 将控制转移到被标记的语句。但是不建议在程序中使用 goto 语句。
goto label;
..
.
label: statement;
边栏推荐
猜你喜欢

Building lightweight target detection based on mobilenet-yolov4

WPF 开源控件库Extended WPF Toolkit介绍(经典)

深潜Kotlin协程(十五):测试 Kotlin 协程

jmeter分布式压测

math_ Number set (number set symbol) and set theory

MATLAB | 基于分块图布局的三纵坐标图绘制
![[BJDCTF2020]The mystery of ip](/img/f8/c3a7334252724635d42c8db3d1bbb0.png)
[BJDCTF2020]The mystery of ip

静态时序分析-OCV和time derate

resnet152 辣椒病虫害图像识别1.0

Anaconda3安装过程及安装后缺失大量文件,没有scripts等目录
随机推荐
Promise [II. Promise source code] [detailed code comments / complete test cases]
Description of replacement with STM32 or gd32
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
【promise一】promise的介绍与手撸的关键问题
In a sense, the Internet has become an incubator and a parent
1.5 use of CONDA
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
WPF open source control library extended WPF toolkit Introduction (Classic)
Cvpr2021:separating skills and concepts for new visual question answering
Easy to use plug-ins in idea
Basic functions of promise [IV. promise source code]
USB DRIVER
Agile development - self use
nignx配置单ip限流
Implementation of window encryption shell
文旅灯光秀打破时空限制,展现景区夜游魅力
How to make ef core 6 support dateonly type
如何系统学习LabVIEW?
渗透测试-文件上传/下载/包含
LDR6028 手机设备一边充电一边OTG传输数据方案