当前位置:网站首页>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;
边栏推荐
- In a sense, the Internet has become an incubator and a parent
- Is the truth XX coming? Why are test / development programmers unwilling to work overtime? This is a crazy state
- [数组]BM94 接雨水问题-较难
- How can e-commerce products be promoted and advertised on Zhihu?
- WPF open source control library extended WPF toolkit Introduction (Classic)
- Qchart note 2: add rollover display
- 微服务系统设计——分布式锁服务设计
- 733. 图像渲染
- iOS开发:对于动态库共享缓存(dyld)的了解
- Microservice system design -- microservice invocation design
猜你喜欢
ERP demand and sales management Kingdee
微服务系统设计——服务注册与发现和配置设计
面对AI人才培养的“产学研”鸿沟,昇腾AI如何做厚产业人才黑土地?
Matlab | drawing of three ordinate diagram based on block diagram layout
微服务系统设计——服务链路跟踪设计
Kotlin compose implicitly passes the parameter compositionlocalprovider
Microservice system design -- unified authentication service design
Cultural tourism light show breaks the time and space constraints and shows the charm of night tour in the scenic spot
fplan-电源规划
Almost because of json Stringify lost his bonus
随机推荐
日志收集系统
Basic functions of promise [IV. promise source code]
Fplan powerplan instance
733. 图像渲染
010 C语言基础:C函数
008 C语言基础:C判断
Fastdds server records - Translation-
Microservice system design -- unified authentication service design
Golang Hello installation environment exception [resolved]
Microservice system design -- microservice invocation design
Almost because of json Stringify lost his bonus
Ledrui ldr6035 usb-c interface device supports rechargeable OTG data transmission scheme.
深潜Kotlin协程(十五):测试 Kotlin 协程
[数组]BM94 接雨水问题-较难
Static timing analysis OCV and time derive
第2章 关键技术介绍
015 C语言基础:C结构体、共用体
A^2=e | the solution of the equation | what exactly can this equation tell us
USB DRIVER
MySql最详细的下载教程