当前位置:网站首页>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;
边栏推荐
- JMeter takes the result of the previous request as the parameter of the next request
- iOS开发:对于动态库共享缓存(dyld)的了解
- Kotlin Compose 隐式传参 CompositionLocalProvider
- Further exploration of handler (Part 2) (the most complete analysis of the core principles of handler)
- Qchart note 2: add rollover display
- Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories
- Semantic version 2.0.0
- 2021:passage retrieval for outside knowledgevisual question answering
- There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused
- 使用promise的基本功能【四、Promise源码】
猜你喜欢

JMeter distributed pressure measurement

MySQL development environment

静态时序分析-OCV和time derate

Stack overflow vulnerability

Kotlin compose compositionlocalof and staticcompositionlocalof
![Promise source code class version [III. promise source code] [detailed code comments / complete test cases]](/img/51/e1c7d5a7241a6eca6c179ac2cb9088.png)
Promise source code class version [III. promise source code] [detailed code comments / complete test cases]

jmeter将上一个请求的结果作为下一个请求的参数

Anaconda3 is missing a large number of files during and after installation, and there are no scripts and other directories

2019LXMERT:Learning Cross-Modality Encoder Representations from Transformers

Nignx configuring single IP current limiting
随机推荐
A^2=E | 方程的解 | 这个方程究竟能告诉我们什么
Argo Workflows —— Kubernetes的工作流引擎入门
2016Analyzing the Behavior of Visual Question Answering Models
通信中的机器学习最佳阅读资料列表
静态时序分析-OCV和time derate
文旅灯光秀打破时空限制,展现景区夜游魅力
Nestjs environment variable configuration to solve the problem of how to inject services into interceptors
ERP demand and sales management Kingdee
WPF open source control library extended WPF toolkit Introduction (Classic)
Argo workflows - getting started with kubernetes' workflow engine
List of best reading materials for machine learning in communication
Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
resnet152 辣椒病虫害图像识别1.0
2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
渗透测试-文件上传/下载/包含
Is the money invested in financial products guaranteed? Is there no more?
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
高等数学(第七版)同济大学 习题1-10 个人解答
MobileNet系列(4):MobileNetv3网络详解
Kotlin compose compositionlocalof and staticcompositionlocalof