当前位置:网站首页>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;
边栏推荐
- 2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
- Fastdds server records - Translation-
- 2021:passage retrieval for outside knowledgevisual question answering
- How to make ef core 6 support dateonly type
- Log collection system
- In a sense, the Internet has become an incubator and a parent
- Kotlin compose implicitly passes the parameter compositionlocalprovider
- 1.5 use of CONDA
- 2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering
- Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
猜你喜欢
2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答
1.5 use of CONDA
Products change the world
Kotlin compose implicitly passes the parameter compositionlocalprovider
Implementation of window encryption shell
Building lightweight target detection based on mobilenet-yolov4
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
如何系统学习LabVIEW?
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
Ldr6028 OTG data transmission scheme for mobile devices while charging
随机推荐
Cultural tourism night tour | stimulate tourists' enthusiasm with immersive visual experience
手机新领域用法知识
如何系统学习LabVIEW?
JMeter distributed pressure measurement
高等数学(第七版)同济大学 习题1-10 个人解答
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
渗透测试-文件上传/下载/包含
[BJDCTF2020]The mystery of ip
面试-01
Cache comprehensive project - seckill architecture
深潜Kotlin协程(十五):测试 Kotlin 协程
A^2=e | the solution of the equation | what exactly can this equation tell us
Il manque beaucoup de fichiers et de répertoires tels que scripts pendant et après l'installation d'anaconda3
fplan-布局
Mobilenet series (4): mobilenetv3 network details
2020:MUTANT: A Training Paradigm for Out-of-Distribution Generalizationin Visual Question Answering
ERP demand and sales management Kingdee
Penetration test - directory traversal vulnerability
[promise I] introduction of promise and key issues of hand rolling
再探Handler(下)(Handler核心原理最全解析)