当前位置:网站首页>C language circular statement
C language circular statement
2022-07-06 03:28:00 【Don't be confused afterwards】
Catalog
One 、 Type of cycle
C The language provides the following types of loops .
| Type of cycle | describe |
|---|---|
| while loop | When a given condition is true , Repeat a statement or group of statements . It tests the condition before executing the loop body . |
| for loop | Execute a sequence of statements multiple times , Simplify the code for managing loop variables . |
| do...while loop | Except that it tests the condition at the end of the loop body , Others and while Statements like . |
| Nested loop | You can go to while、for or do..while Use one or more loops within the loop . |
for Loop compiler error
Some compilers will report errors when running the above statements , as follows :
[Error] 'for' loop initial declarations are only allowed in C99 or C11 modeintend “ Do not allow the for Internally defined variables ”.
Change it to :
int a=10;
for(; a<20; a=a+1){
}Then it can run .
If you are using Dev C++ The above problems occur , You can find tools directly above the editor --> Compilation options --> The generated code / Optimize --> Code generation --> Language standard ISO C99 Then click below to save .
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 will ensure that At least once loop .
grammar
do
{
statement(s);
}while( condition );example
// example
#include <stdio.h>
int main ()
{
/* Definition of local variables */
int a = 10;
/* do Loop execution , Execute at least once before the condition is tested */
do
{
printf("a Value : %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}When the above code is compiled and executed , It will produce the following results :
a Value : 10
a Value : 11
a Value : 12
a Value : 13
a Value : 14
a Value : 15
a Value : 16
a Value : 17
a Value : 18
a Value : 19C Nesting in languages do...while The syntax of circular statements :
do
{
statement(s);
do
{
statement(s);
... ... ...
}while (condition2);
... ... ...
}while (condition1example
//do-while Nested instances
#include <stdio.h>
int main()
{
int i=1,j;
do
{
j=1;
do
{
printf("*");
j++;
}while(j <= i);
i++;
printf("\n");
}while(i <= 5);
return 0;
}When the above code is compiled and executed , It will produce the following results :
*
**
***
****
*****Two 、 Loop control statement
Loop control statements change the execution order of the code . It can realize the jump of code .C The following loop control statements are provided .
| Control statement | describe |
|---|---|
| break sentence | End loop or switch sentence , The program flow will continue to execute immediately following the loop or switch The next sentence of . |
| continue sentence | Tell a loop body to stop this iteration immediately , Restart the next iteration of the loop . |
| goto sentence | Transfer control to the marked statement . however It is not recommended to use goto sentence . |
break sentence
C In language break Statement has the following two uses :
- When break When a statement appears in a loop , The loop ends immediately , And the program flow continues to execute the next statement that follows the loop .
- It can be used to terminate switch One of the sentences case.
If you are using nested loops ( That is, one loop embeds another ),break Statement will stop executing the innermost loop , Then start executing the next line of code after the block .
grammar
break;
example
// example
#include <stdio.h>
int main ()
{
/* Definition of local variables */
int a = 10;
/* while Loop execution */
while( a < 20 )
{
printf("a Value : %d\n", a);
a++;
if( a > 15)
{
/* Use break Statement to terminate the loop */
break;
}
}
return 0;
}When the above code is compiled and executed , It will produce the following results :
a Value : 10
a Value : 11
a Value : 12
a Value : 13
a Value : 14
a Value : 15continue sentence
C In language continue It's a little bit like that break sentence . But it's not a mandatory termination ,continue Code in the current loop will be skipped , Force to start the next cycle .
about for loop ,continue After the statement is executed, the auto increment statement will still execute . about while and do...while loop ,continue Statement to re execute the conditional judgment statement .
grammar
continue;
example
// example
#include <stdio.h>
int main ()
{
/* Definition of local variables */
int a = 10;
/* do Loop execution */
do
{
if( a == 15)
{
/* Skip iteration */
a = a + 1;
continue;
}
printf("a Value : %d\n", a);
a++;
}while( a < 20 );
return 0;
}When the above code is compiled and executed , It will produce the following results :
a Value : 10
a Value : 11
a Value : 12
a Value : 13
a Value : 14
a Value : 16
a Value : 17
a Value : 18
a Value : 193、 ... and 、 Infinite loop
If the conditions are never false , Then the loop will become an infinite loop .for Loop can be used to realize infinite loop in traditional sense . Because none of the three expressions that make up the loop is necessary , You can leave some conditional expressions blank to form an infinite loop .
// example
#include <stdio.h>
int main ()
{
for( ; ; )
{
printf(" The cycle will go on forever !\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 .
边栏推荐
- Restful style
- Yyds dry inventory what is test driven development
- 给新人工程师组员的建议
- Pytorch基础——(1)张量(tensor)的初始化
- Overview of OCR character recognition methods
- pytorch加载数据
- How to write compile scripts compatible with arm and x86 (Makefile, cmakelists.txt, shell script)
- Shell 传递参数
- Pelosi: Congress will soon have legislation against members' stock speculation
- 真机无法访问虚拟机的靶场,真机无法ping通虚拟机
猜你喜欢

2.2 STM32 GPIO操作

SAP ALV颜色代码对应颜色(整理)

Map sorts according to the key value (ascending plus descending)

银行核心业务系统性能测试方法

Cubemx 移植正点原子LCD显示例程

Force buckle 1189 Maximum number of "balloons"

Tidb ecological tools (backup, migration, import / export) collation

Microsoft Research, UIUC & Google research | antagonistic training actor critic based on offline training reinforcement learning

Four logs of MySQL server layer

1. New project
随机推荐
SAP ALV单元格级别设置颜色
BUUCTF刷题笔记——[极客大挑战 2019]EasySQL 1
1、工程新建
[Li Kou] the second set of the 280 Li Kou weekly match
Résumé des méthodes de reconnaissance des caractères ocr
2. GPIO related operations
1.16 - check code
Explore pointers and pointer types in depth
记录一下逆向任务管理器的过程
3857 Mercator coordinate system converted to 4326 (WGS84) longitude and latitude coordinates
Safety science to | travel, you must read a guide
StrError & PERROR use yyds dry inventory
ASU & OSU | model based regularized off-line meta reinforcement learning
Cross origin cross domain request
Buuctf question brushing notes - [geek challenge 2019] easysql 1
施努卡:3d视觉检测应用行业 机器视觉3d检测
Pytorch基础——(1)张量(tensor)的初始化
【Rust 笔记】18-宏
Leetcode problem solving -- 98 Validate binary search tree
MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置