当前位置:网站首页>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;
边栏推荐
- Games101 job 7 improvement - implementation process of micro surface material
- 苹果手机证书构体知识
- Stack overflow vulnerability
- Kotlin Compose 隐式传参 CompositionLocalProvider
- How can e-commerce products be promoted and advertised on Zhihu?
- 2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers
- MySql最详细的下载教程
- 再探Handler(上)(Handler核心原理最全解析)
- Ldr6028 OTG data transmission scheme for mobile devices while charging
- Fplan layout
猜你喜欢

Mobilenet series (4): mobilenetv3 network details

How can e-commerce products be promoted and advertised on Zhihu?

MySQL development environment

Kotlin compose compositionlocalof and staticcompositionlocalof
![[BJDCTF2020]The mystery of ip](/img/f8/c3a7334252724635d42c8db3d1bbb0.png)
[BJDCTF2020]The mystery of ip

CVPR2021:Separating Skills and Concepts for Novel Visual Question Answering将技巧与概念分开的新视觉问答

如何让 EF Core 6 支持 DateOnly 类型

低代码开发平台NocoBase的安装

Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf

Learn crypto from Buu (Zhou Geng)
随机推荐
JMeter distributed pressure measurement
Nacos调用微服务两个问题:1.Load balancer does not contain an instance for the service 2.Connection refused
List of best reading materials for machine learning in communication
2016Analyzing the Behavior of Visual Question Answering Models
跟着BUU学习Crypto(周更)
苹果唯一图谱架构常识
缓存综合项目--秒杀架构
Anaconda3安裝過程及安裝後缺失大量文件,沒有scripts等目錄
乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
Installation of low code development platform nocobase
022 C语言基础:C内存管理与C命令行参数
Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
文旅夜游|以沉浸式视觉体验激发游客的热情
Cvpr2021:separating skills and concepts for new visual question answering
2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
真xx相来了?测试/开发程序员为什么不愿意加班,这是个疯狂的状态......