当前位置:网站首页>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;
边栏推荐
- Basic functions of promise [IV. promise source code]
- Microservice system design -- Distributed timing service design
- How to make ef core 6 support dateonly type
- Learn crypto from Buu (Zhou Geng)
- 1.5 use of CONDA
- Système de collecte des journaux
- Knowledge of iPhone certificate structure
- Usage knowledge of mobile phones in new fields
- Log collection system
- 日志收集系統
猜你喜欢

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

微服务系统设计——服务注册与发现和配置设计

Nignx configuring single IP current limiting

Argo workflows - getting started with kubernetes' workflow engine

电商产品如何在知乎上进行推广和打广告?
![[array]bm94 rainwater connection problem - difficult](/img/2b/1934803060d65ea9139ec489a2c5f5.png)
[array]bm94 rainwater connection problem - difficult

Agile development - self use

mysql数据库基础:DQL数据查询语言

Facing the "industry, University and research" gap in AI talent training, how can shengteng AI enrich the black land of industrial talents?

Kotlin Compose 隐式传参 CompositionLocalProvider
随机推荐
MySql的开发环境
1.5 conda的使用
乐得瑞LDR6035 USB-C接口设备支持可充电可OTG传输数据方案。
Kotlin Compose 自定义 CompositionLocalProvider CompositionLocal
文旅灯光秀打破时空限制,展现景区夜游魅力
Office VR porn, coquettish operation! The father of Microsoft hololens resigns!
[array]bm94 rainwater connection problem - difficult
Kotlin Compose 隐式传参 CompositionLocalProvider
LDR6028 手机设备一边充电一边OTG传输数据方案
Matlab | visualization of mathematical properties related to three interesting circles
微服务系统设计——服务链路跟踪设计
013 C语言基础:C指针
跟着BUU学习Crypto(周更)
低代码开发平台NocoBase的安装
iOS开发:对于动态库共享缓存(dyld)的了解
1.5 use of CONDA
与STM32或GD32替换说明
Quickly master asp Net authentication framework identity - reset password by mail
Common sense of Apple's unique map architecture
Microservice system design -- microservice monitoring and system resource monitoring design