当前位置:网站首页>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
- 手机新领域用法知识
- IDEA中好用的插件
- 再探Handler(上)(Handler核心原理最全解析)
- 2021:Check it again:Progressive Visual Question Answering via Visual Entailment通过视觉暗示进行渐进式视觉问答
- Servlet and JSP final review examination site sorting 42 questions and 42 answers
- 渗透测试-文件上传/下载/包含
- [数组]BM94 接雨水问题-较难
- 快速掌握 ASP.NET 身份认证框架 Identity - 通过邮件重置密码
- Knowledge of iPhone certificate structure
猜你喜欢

2021:Graphhopper: Multi-Hop Scene Graph Reasoning for Visual Question Answering

math_数集(数集符号)和集合论

There are two problems when Nacos calls microservices: 1 Load balancer does not contain an instance for the service 2. Connection refused

Products change the world

Kotlin compose compositionlocalof and staticcompositionlocalof

Mysql database foundation: DQL data query language

渗透测试-文件上传/下载/包含

2021:passage retrieval for outside knowledgevisual question answering

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

Why does C throw exceptions when accessing null fields?
随机推荐
IOS development: understanding of dynamic library shared cache (dyld)
WPF open source control library extended WPF toolkit Introduction (Classic)
Kotlin compose implicitly passes the parameter compositionlocalprovider
Système de collecte des journaux
如何系统学习LabVIEW?
fplan-Powerplan实例
Static timing analysis OCV and time derive
jmeter将上一个请求的结果作为下一个请求的参数
2021:Beyond Question-Based Biases:Assessing Multimodal Shortcut Learning in Visual Question Answeri
USB DRIVER
windows上安装MySQL
A^2=e | the solution of the equation | what exactly can this equation tell us
MySql的开发环境
fplan-电源规划
人间清醒:底层逻辑和顶层认知
静态时序分析-OCV和time derate
MobileNet系列(4):MobileNetv3网络详解
Argo Workflows —— Kubernetes的工作流引擎入门
2021:AdaVQA: Overcoming Language Priors with Adapted Margin Cosine Loss∗自适应的边缘余弦损失解决语言先验
Resnet152 pepper pest image recognition 1.0