当前位置:网站首页>[Xiao Yang takes you to play with C language] circular structure (detailed explanation)
[Xiao Yang takes you to play with C language] circular structure (detailed explanation)
2022-04-22 02:30:00 【Xiao Yang AI programming*】

List of articles
Each preface
Each preface
1. Introduction
2. The goal is
3. a key
1.1,while Sentence basic syntax
1.4,while Jump statement continue
1.5,while Jump statement break
2.1,do-while Sentence basic syntax
2.2,do-while Recycling example
2.3,do-while Precautions for use
2.4,do-while Jump statement continue
2.5,do-while Jump statement break
3.4,for Jump statement continue
4.1, Loop nesting basic syntax
4.2, Examples of circular nesting
4.3, Precautions for circular nesting
4. Conclusion
Each preface
Blog home page : Xiao Yang AI programming *
Author's brief introduction :2022 Blog Mengxin in
My motto : Those who have the will have the long will , He who has no will is determined to
The author requests : Due to the limited level of bloggers , There will inevitably be mistakes and inaccuracies , I hope you can criticize me in the message area .
Introduction
This blog mainly introduces you in detail C The circular structure of language . I hope you can read this blog , Benefit a lot !
The goal is
Understand and master C Knowledge of the circular structure of language , Have a new understanding and understanding of circular structure .
Knowledge point
while Loop statement
1.1,while Sentence basic syntax
Sentence format :

Execute the process :

Process analysis :
1. First step : Evaluate the value of an expression , If the expression value is true , Go to step two , If it's false , Then jump to step 4 .
2. The second step : Execute loop body statement .
3. The third step : Step one back .
4. Step four : End of cycle , perform while The next statement of a statement .
Be careful :while Characteristics of sentences : Execute the judgment condition first , Post execution loop body
1.2,while Recycling example
Example : Print 1-10 The number of
The code is as follows :
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
printf("%d ", i);
i++;
}
return 0;
}
Sample output :

1.3,while Precautions for use
1,while Parentheses after the statement "( )" Do not omit .
2,while The expression in the statement can be any type of expression , If the value of the expression is not 0, Indicates that the cycle condition is true , Execute loop body statement ; If the value of the expression is 0, Indicates that the cycle condition is false , Do not execute loop body statements , End of cycle , Execute the statement after the loop .
3, If the expression starts with 0, Then the loop body does not execute once , Directly execute the statement after the loop body .
4, The loop body statement can be a compound statement , That is, when the loop body is composed of multiple statements , The statement must be enclosed in a brace , Form compound statements .
5, In order for the cycle to end normally , The expression should be modified in the loop body , Make the value of the expression tend to be false , Avoid a dead cycle .
1.4,while Jump statement continue
Examples are as follows :
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 5)
{
continue;
}
printf("%d ", i);
i++;
}
return 0;
}
Sample output :

Sample analysis :
In this while In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform continue sentence ,continue Statement will make this while Cycle termination ,continue The latter code does not execute , Direct transfer to while The judgment part of a sentence ,i Not in progress i++ operation ,i Always be 5, So this program will fall into an endless loop , The output results are printed out 1 2 3 4 Post dead cycle .
summary :
1,continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed , It's a jump to while The judgment part of a sentence . Make the entry judgment of the next cycle .
2,continue The sentence can only be in a circular sentence , Often with if Statements use .
1.5,while Jump statement break
Examples are as follows :
#include<stdio.h>
int main()
{
int i = 1;
while (i <= 10)
{
if (i == 5)
{
break;
}
printf("%d ", i);
i++;
}
return 0;
}
Sample output :

Sample analysis :
In this while In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform break sentence ,break Statement will make this while Cycle termination , The following steps are not carried out , So this program will only print out 1 2 3 4.
Be careful :i++ The position of this statement has an impact on the result of the program output , Different placement positions , The output results may be different .
summary :
1, When break Statement for while In loop statement , It can cause the program to terminate the loop and then execute while The statement follows the statement . In short , In the loop, whenever you encounter break, Just terminate the loop . therefore :while Medium break It's used to terminate the loop permanently .
2, Usually ,break Sentences are always related to if Statements , That is, exit the cycle when the conditions are met .
do-while Loop statement
2.1,do-while Sentence basic syntax
Sentence format :

Execute the process :

Process analysis :
1, First step : Execute loop body statement .
2, The second step : Evaluate the value of an expression , If the value of the expression is true ( Not 0), Step one back ; If the value of the expression is false (0), Then go to step three .
3, The third step : End of cycle , perform do-while The next statement of a statement .
Be careful :do-while The statement is characterized by executing the loop body first , Post judgment cycle conditions . therefore ,do-while The loop must be executed at least once .
2.2,do-while Recycling example
Example : Print 1-10 The number of
Sample code :
#include<stdio.h>
int main()
{
int i = 1;
do
{
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
Sample output :

2.3,do-while Precautions for use
1, stay do There can be no statement terminator after ";", Because the cycle is not over yet .
2, stay while( expression ) After that, there must be a statement Terminator ";", Express do-while This is the end of the loop .
3, There must be a statement in the loop body to change the loop condition , Otherwise, there will be a dead cycle .
2.4,do-while Jump statement continue
Sample code :
#include<stdio.h>
int main()
{
int i = 1;
do
{
if (i == 5)
{
continue;
}
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
Sample output :

Sample analysis :
In this do-while In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform continue sentence ,continue Statement will make this do-while Cycle termination ,continue The latter code does not execute , Direct transfer to while The judgment part of a sentence ,i Not in progress i++ operation ,i Always be 5, So this program will fall into an endless loop , The output results are printed out 12 3 4 Post dead cycle .( Use the same while The cycle is similar )
Be careful :i++ The position of this statement has an impact on the result of the program output , Different placement positions , The output results may be different .
summary :
1,continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed , It's a jump to while The judgment part of a sentence . Make the entry judgment of the next cycle .
2,continue The sentence can only be in a circular sentence , Often with if Statements use .
2.5,do-while Jump statement break
Examples are as follows :
#include<stdio.h>
int main()
{
int i = 1;
do
{
if (i == 5)
{
break;
}
printf("%d ", i);
i++;
} while (i <= 10);
return 0;
}
Sample output :

Sample analysis :
In this do-while In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform break sentence ,break Statement will make this do-while Cycle termination , The following steps are not carried out , So this program will only print out 1 2 3 4.( Use the same while The cycle is the same )
summary :
1, When break Statement for do-while In loop statement , It can cause the program to terminate the loop and then execute while The statement follows the statement . In short , In the loop, whenever you encounter break, Just terminate the loop .
2, Usually ,break Sentences are always related to if Statements , That is, exit the cycle when the conditions are met .
for Loop statement
3.1,for Sentence basic syntax
Sentence format :

Be careful :
expression 1 For the initialization part , Used to initialize loop variables .
expression 2 For the conditional judgment part , Used to determine whether the loop is terminated .
expression 3 For the adjustment part , For adjustment of cyclic conditions .
Execute the process :

Process analysis :
1. First step : First calculate the expression 1.
2. The second step : Judging expressions 2, If the value is true ( Not 0), The loop body statement is executed , And then step three ; If it's false (0), End of cycle , Go to step 5 .
3. The third step : Calculation expression 3.
4. Step four : Return to step 2 and continue .
5. Step five : The loop ends , Carry on for The next statement of a statement .
Be careful : expression 1 Just calculated once before entering the cycle . expression 2, Loop body statements and expressions 3 Will repeat .
3.2,for Recycling example
Example : Print 1-10 The number of
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf("%d ", i);
}
return 0;
}
Sample output :

3.3,for Precautions for use
1, Omit expression 1, You can assign the initial value of the cyclic variable to for Before , But the first one cannot be omitted ";".
2, Omit expression 2, Indicates that the loop is not controlled , At this time, if there is no other treatment, there will be an dead cycle , This should be avoided .
3, Omit expression 3, You can add a statement that modifies the value of the loop variable to the loop body sentence .
4,for An expression in the general form of a statement 1 And expressions 3 It can be a comma expression .
5,for An expression in the general form of a statement 2 As long as the value of is not 0, Execute the loop body .
6,for The circular body statement in the general form of the statement can be omitted .
3.4,for Jump statement continue
Sample code :
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Sample output :

Sample analysis :
In this for In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform continue sentence ,continue Statement will make this for Cycle termination ,continue The latter code does not execute , Direct transfer to for The judgment part of the loop statement , Not printed 5 The operation of this number ,i Decide to proceed again according to the result of the judgment part i++ operation , In turn, print 6 7 8 9 10, So the final output of this program is to print out 1 2 3 4 6 7 8 9 10, The lack of 5.( And while Circulation and do-while Circular usage is different )
summary :
1,continue Is used to terminate the cycle , In this cycle continue The following code is no longer executed , It's a jump to while The judgment part of a sentence . Make the entry judgment of the next cycle .
2,continue The sentence can only be in a circular sentence , Often with if Statements use .
3.5,for Jump statement break
Sample code :
#include<stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
if (i == 5)
{
break;
}
printf("%d ", i);
}
return 0;
}
Sample output :

Sample analysis :
In this for In circulation ,i from 1 After printing in turn ++. When the program prints 4 after , The program is executed again i++ After this code i The value of is 5, When i==5 when , perform break sentence ,break Statement will make this while Cycle termination , The following steps are not carried out , So this program will only print out 1 2 3 4.( And while Circulation and do-while Circular usage is similar )
summary :
1, When break Statement for while In loop statement , It can cause the program to terminate the loop and then execute while The statement follows the statement . In short , In the loop, whenever you encounter break, Just terminate the loop .
2, Usually ,break Sentences are always related to if Statements , That is, exit the cycle when the conditions are met .
A nested loop
4.1, Loop nesting basic syntax
Sentence format :
-
for Nested in statement for sentence

-
for Nested in statement while sentence

-
while Nested in statement while sentence

-
do-while Nested in statement for sentence

-
do-while Nested in statement do-while sentence

-
while Nested in statement do-while sentence

4.2, Examples of circular nesting
Example : Print the multiplication table
Sample code :
#include<stdio.h>
int main()
{
for (int i = 1; i <= 9; i++)
{
for (int j = 1; j <= i; j++)
{
printf("%d*%d=%2d ",j , i, i * j);
}
printf("\n");
}
return 0;
}
Sample output :

4.3, Precautions for circular nesting
1, The outer loop is executed once , The inner loop executes a round .
2, The inner and outer loop bodies in the loop nested format are not allowed to cross , That is, the outer loop should completely include the inner loop .
3, Loop nesting , Used in internal circulation break Statement and continue When the sentence is , Only the inner loop containing them is affected , It has nothing to do with external circulation .
goto Jump statements
goto sentence : You can abuse and mark jump at will . Theoretically goto Statements are not necessary , But on some occasions goto Statements are still useful , The most common use is to terminate the processing of a program's deeply nested structure .
for example : Jump out of two or more loops at a time .
Multilayer loops are used in this case break Can't achieve the goal .break You can only exit from the innermost loop to the previous loop .
Conclusion
Dear friends ,【 Xiao Yang will show you around C Language 】 Loop structure ( Detailed explanation ) That's it , In the next issue, I will explain the function part to you . Coming soon !
If you guys feel good about it , Or some harvest after reading , Please move your little hands , Give me a three company ( Focus on , give the thumbs-up , Comment on ), Give me more support !!
friends , Bear in mind ! These sample code must remember to actually operate , Only in this way can we deepen our understanding of these knowledge !
Last , Thank you for reading , Let's work together , Come on together , Progress together !
版权声明
本文为[Xiao Yang AI programming*]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220226354055.html
边栏推荐
- Dump mangodb data using Navicat
- 编程主要学什么
- AI应用说-智慧农场(牛场无人监控)
- JS Baidu map positioning
- Excel tips - vlookup automatic matching
- [PROJECT] small hat takeout (VII)
- Can you really use ` timescale?
- XSS cross site script attack learning record
- A general tool class for adding, deleting, querying and modifying databases based on Hikari connection pool
- Pychar executes multiple script files at the same time
猜你喜欢

Information Security Overview

AI application theory - Smart farm (cattle farm without monitoring)

49 pages enterprise digital transformation cases and common tools enterprise digital transformation ideas

Mysql的索引为什么使用B+树而不使用跳表?

72 page Internet smart Park solution

Softmax operation

YOLOv3论文翻译

How to generate anr in service?

嵌入式AI

风控产品定额的陷阱都有哪些?
随机推荐
golang 1.8泛型测试
Scala installation and environment configuration
Plug in import and usage of uni list
How to select the appropriate neo4j Version (2022)
Swift 泛型的使用
3D album template (size can be changed)
Ren Jie, chief scientist of rongyun: the Internet is unstoppable, but there are always young people
OpenCV计算图像的梯度特征
Eight common probability distribution formulas and visualization
Another way to write the fluent interface is to write a part first, then material, and put the method body in the method body
开发管理·华为IPD
Unity Game Optimization - Third Edition 阅读笔记 Chapter 1 分析性能问题
softmax运算
Mongodb grouping query
互联网行业为什么能吸引越来越多的年轻人?尤其是程序员……
像堆乐高一样解释神经网络的数学过程
Line feed in string value when sqlserver parses JSON
SQLSERVER解析JSON时string value中换行符问题
13. Installation mode of system software
Use of swift generics