当前位置:网站首页>[C language - zero foundation lesson 8] circular structure and break continue
[C language - zero foundation lesson 8] circular structure and break continue
2022-07-27 09:15:00 【Super Daxiong】
Preface
Blogger :Super Daxiong ( A cute new blogger )
C Language column :0 Basic science C Language column
LeetCode special column :LeetCode special column
This issue is about C Language cycle structure and break continue, If there is any mistake, you are welcome to put forward your views .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
Catalog
The most important thing in life is circulation , The earth goes around the sun 、 a week 7 God 、 One day 24 Hours 、 An hour 60 Minutes, this is the cycle . stay C There are also cycles in language . If I let you use C Language promises 100 strip “ I like C Language ”, What method would you choose , I don't think you want to play 100 strip print(“ I like C Language \n”); Too tired , And no brain . So the circular structure we learn is used to solve this problem , Loop statements have while、do-while、for.
while() Loop statement
Format
While( Conditions ){
sentence ;
}
conditions for execution : Judge the conditions first , When the condition is true, the loop statement is executed , End the loop until the condition is false .
int i = 1;
while (i<=10)
{
printf("%d",i);
i++;
}result :12345678910
There are three elements of circulation : The initial value of the cycle 、 The loop condition 、 Cycle increment ( Step value )
do-while sentence
Format :do sentence
While( Conditions );
Execution process : Execute statement first , In judging conditions , When the condition is true, continue to execute the loop statement , Until the condition is false .
int i = 1;
do
printf("%d", i);
while (i>5);result :1
int i = 1;
do
printf("%d",i++);
while (i<5);result :1234 i++ The post value usage is used
Do-while and while The difference between loop statements
Do-while Execute the loop at least once ,while The statement may not be executed once .
Do-while The end sign of the loop statement is ;
for() Loop statement
Format
For( Conditions for a : initial value ; Condition 2 : Conditions ; Condition 3 : The incremental ){
sentence ;
}
for ( int i = 0; i <= 100; i++)
{
printf("%d\n",i);
}result : Print out 0-100 Value
Calculation 1-100 Summation of
int sum = 0;
for ( int i = 1; i <= 100; i++)
{
sum += i;
}
printf("1-100 The cumulative sum of is %d",sum);result : 1-100 The cumulative sum of is 5050
For The characteristics of the cycle : It is mostly used to specify the initial value and known termination conditions 、 The number of cycles is controllable
Get into for Loop executes the statement of loop one first ( stay for Only execute once in the loop ), Then judge the conditions , If it's true, it goes into a cycle , Then execute condition 3 next time —— Whether condition two is true, and then in this order , Until the conditions don't hold .
Break and continue
Break sentence
Break sentence : Jump out and terminate the current loop .
No, break The sentence of
for (int i = 1;i <= 5;i++) {
printf("%d\n",i);
}result :
1
2
3
4
5
Yes break The sentence of
stay for In circulation
for (int i = 1;i <= 5;i++) {
printf("%d\n",i);
break;
}result :
1
The program only executes once and jumps out of the loop ;
for (int i = 1;i <= 5;i++) {
if (i == 3) {
break;
}
printf("%d\n",i);
}result :
1
2
The program only executes until the judgment condition is established and directly jumps out for loop ;
stay while In circulation
int i = 1;
while (1) {// Not 0 It's all true , therefore while The condition is always true
if (i > 5) { i>5 sign out while loop
break;
}
printf("%d\n",i++);
}result :
1
2
3
4
5
stay switch In circulation
We can use break Calculate the number of days per month , Because in switch There is no break Just keep going , Until I met break stop it .
int i ;
scanf("%d",&i);
switch (i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: printf("31");break;
case 2:
case 4:
case 6:
case 9:
case 11:printf("30");break;
default:
printf("28 or 29");
break;
}continue sentence
continue: End this cycle and proceed to the next cycle
stay for In circulation
for (int i = 0;i <= 5;i++) {
if (i == 4)continue;
printf("%d",i);
}result :01235
stay while In circulation
int i = 0;
while (i++<5)
{
if (i == 4)continue;
printf("%d",i);
}result :1235
continue: Do not use... In selection statements (switch sentence ), Use only in loop statements .
A nested loop
What is nesting : A circular sentence contains another circular sentence , For example, you are watching TV now and the people on the TV screen are watching TV .
example 1:
for (int i = 0; i < 2; i++){
for (int j = 0; j < 1; j++)
{
printf("%d,%d\n", i, j);
}
}result :
0,0
1,0
analysis : We can see the first output 0,0 That is, execute the outer layer first for loop i=0, Then enter the inner layer for loop i=0, Output 0,0 Then the inner layer for loop j+1 be equal to 1,1<1 If the condition is not satisfied, the execution will be executed for loop ,i=1; Then the inner layer for loop j=0 Then the output 1,0 Then the inner layer for The cycle is equal to 1 Exit if the conditions are not met for loop i=2 Exit the loop if the conditions are not met .
example 2:
for (int i = 0; i < 1; i++){
for (int j = 0; j < 5; j++)
{
if (i == 3) {
break;
}
printf("j=%d\n", j);
}
printf("i=%d\n", i);
}result :
j=0
j=1
j=2
j=3
j=4
i=0
break; A statement is to jump out of the nearest for Cyclic .
Recommend one to my friends Study 、 Brush problem Website ?
Various Interview questions have everything that one expects to find , Brush questions until you feel soft !
All kinds of Internet Learning materials , The real interview questions of major factories start from Start learning from scratch , Help you cope easily Various interview questions , Come and enrich yourself !
边栏推荐
- Programming style
- Explanation of binary tree
- [daily algorithm day 96] Tencent interview question: merge two ordered arrays
- Matlab drawing skills and examples: stackedplot
- 函数防抖节流
- Tensorflow package tf.keras module construction and training deep learning model
- How to study happily on station B?
- QT uses SQLite to open multiple database files at the same time
- QDoubleValidator不生效问题解决办法
- DNS域名空间
猜你喜欢

How to optimize the deep learning model to improve the reasoning speed

Can "Gulangyu yuancosmos" become an "upgraded sample" of China's cultural tourism industry

罗克韦尔AB PLC 通过RSLinx Classic与PLC建立通信的具体方法步骤

Solve the problem of Chinese garbled code on the jupyter console

基于restful页面数据交互

08_ Service fusing hystrix

【ACL2020】一种新颖的成分句法树序列化方法
![[micro service ~sentinel] sentinel dashboard control panel](/img/df/2fbe7826ea2b80a81d29351052ae28.png)
[micro service ~sentinel] sentinel dashboard control panel

【进程间通信IPC】- 信号量的学习

Five kinds of 2D attention finishing (non local, criss cross, Se, CBAM, dual attention)
随机推荐
flex布局 (实战小米官网)
npm和yarn 更新依赖包
Interface test tool -postman usage details
Ctfshow ultimate assessment
Tensorflow loss function
一些实用、常用、效率越来越高的 Kubernetes 别名
【进程间通信IPC】- 信号量的学习
New year's goals! The code is more standardized!
Babbitt | yuan universe daily must read: Guangzhou Nansha released the "Yuan universe nine" measures, and the platform can obtain up to 200million yuan of financial support
Explanation of common basic controls for C # form application (suitable for Mengxin)
ES6 new - function part
< script> detailed explanation of label content
易语言编程: 让读屏软件可获取标签控件的文本
5g failed to stimulate the development of the industry, which disappointed not only operators, but also mobile phone enterprises
D3.v3.js data visualization -- pictures and tips of force oriented diagram
Programming style
软件测试功能测试全套常见面试题【功能测试-零基础】必备4-1
Antdesign a-modal user-defined instruction realizes dragging and zooming in and out
SQL exercise set
BEVFormer: Learning Bird’s-Eye-View Representation from Multi-Camera Images via Spatiotemporal Trans