当前位置:网站首页>009 basics of C language: C loop
009 basics of C language: C loop
2022-06-27 04:24:00 【Prison plan progress 50%】
List of articles
One : summary
sometimes , You may need to execute the same piece of code multiple times . In general , Statements are executed sequentially : The first statement in the function is executed first , And then there's the second statement , And so on .
Two :while loop
When a given condition is true , Repeat a statement or group of statements . It tests the condition before executing the loop body .
while(){
}
example :
#include <stdio.h>
int main(){
int a = 10;
while(a<20){
printf("a is : %d \n", a);
a++;
}
return 0;
}
result :
┌──(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
3、 ... and :for loop
Execute a sequence of statements multiple times , Simplify the code for managing loop variables .
for ( init; condition; increment )
{
statement(s);
}
Here is for The control flow of the loop :
init Will be executed first , And only once . This step allows you to declare and initialize any loop control variables . You can also write nothing here , As long as a semicolon appears .
Next , Will judge condition. If it is true , Then the loop body . If it is false , The loop body is not executed , And the control flow will jump to the next for The next statement of the loop .
The execution of the for After cycling the subject , Control flow will jump back up increment sentence . This statement allows you to update the loop control variables . This statement can be left blank , As long as a semicolon appears after the condition .
The condition is judged again . If it is true , Then execute the loop , The process repeats itself ( Cycle subject , Then increase the step value , Then judge the conditions again ). When the condition becomes false ,for Cycle termination .
example :
#include <stdio.h>
int main(){
for(int a = 10; a < 20; a = a+1){
printf("a is : %d \n", a);
}
return 0;
}
result :
┌──(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
Four :do…while loop
Unlike for and while loop , They test the loop conditions at the loop head . stay C In language ,do…while A loop is to check its condition at the end of the loop .do…while Circulation and while Circulation similar , however do…while The loop ensures that the loop is executed at least once .
do
{
statement(s);
}while( condition );
example :
#include <stdio.h>
int main(){
int a = 10;
do{
printf("a is : %d \n", a);
a = a + 1;
}while(a < 20);
return 0;
}
result :
┌──(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
5、 ... and : Nested loop
Can be in while、for or do…while Use one or more loops within the loop .
grammar :
C In language nesting for loop Sentence syntax :
for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
C In language nesting while loop Sentence syntax :
while(condition)
{
while(condition)
{
statement(s);
}
statement(s);
}
C In language nesting do...while loop Sentence syntax :
do
{
statement(s);
do
{
statement(s);
}while( condition );
}while( condition );
6、 ... and : Infinite loop
#include <stdio.h>
int main ()
{
for( ; ; )
{
printf("hello cccccccc \n");
}
return 0;
}
When the conditional expression does not exist , It is assumed to be true . You can also set an initial value and an incremental expression , But in general ,C Programmers tend to use for(;;) Structure to represent an infinite loop .
Be careful : You can press Ctrl + C Key to terminate an infinite loop .
7、 ... and : Loop control statement
Control statement describe
break sentence End loop or switch sentence , The program flow will continue to execute immediately after loop or switch The next sentence of .
break;
continue sentence Causes the loop to skip the rest of the body , Immediately restart the test conditions .
continue;
goto sentence Transfer control to the marked statement . But it is not recommended to use goto sentence .
goto label;
..
.
label: statement;
边栏推荐
- IOS development: understanding of dynamic library shared cache (dyld)
- WPF 开源控件库Extended WPF Toolkit介绍(经典)
- 低代码开发平台NocoBase的安装
- 如何让 EF Core 6 支持 DateOnly 类型
- Microservice system design -- Distributed timing service design
- ERP demand and sales management Kingdee
- [promise I] introduction of promise and key issues of hand rolling
- 009 C语言基础:C循环
- A^2=e | the solution of the equation | what exactly can this equation tell us
- Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf
猜你喜欢

微服务系统设计——服务链路跟踪设计

百度飞桨“万有引力”2022首站落地苏州,全面启动中小企业赋能计划

Almost because of json Stringify lost his bonus

fplan-电源规划

Kotlin Compose compositionLocalOf 与 staticCompositionLocalOf

电商产品如何在知乎上进行推广和打广告?

微服务系统设计——服务熔断和降级设计

Kotlin compose compositionlocalof and staticcompositionlocalof

Learn crypto from Buu (Zhou Geng)

如何系统学习LabVIEW?
随机推荐
微服务系统设计——统一鉴权服务设计
与STM32或GD32替换说明
2022-06-26: what does the following golang code output? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { type
Cultural tourism night tour | stimulate tourists' enthusiasm with immersive visual experience
009 C语言基础:C循环
Baidu PaddlePaddle's "universal gravitation" first stop in 2022 landed in Suzhou, comprehensively launching the SME empowerment plan
Microservice system design -- unified authentication service design
015 C语言基础:C结构体、共用体
Fplan power planning
Log collection system
LDR6028 手机设备一边充电一边OTG传输数据方案
List of best reading materials for machine learning in communication
2022-06-26:以下golang代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { type
011 C语言基础:C作用域规则
Ledrui ldr6035 usb-c interface device supports rechargeable OTG data transmission scheme.
Microservice system design -- Distributed timing service design
微服务系统设计——服务熔断和降级设计
Games101 job 7 improvement - implementation process of micro surface material
微服务系统设计——服务注册与发现和配置设计
A^2=E | 方程的解 | 这个方程究竟能告诉我们什么