当前位置:网站首页>Part I - Fundamentals of C language_ 4. Procedure flow structure
Part I - Fundamentals of C language_ 4. Procedure flow structure
2022-07-19 09:32:00 【qq_ forty-three million two hundred and five thousand two hundr】
4.1 summary
C The language supports three basic program running structures : Sequential structure 、 Selection structure 、 Loop structure .
- Sequential structure : The program is executed in sequence , No jump .
- Selection structure : According to whether the conditions are met , Selectively perform corresponding functions .
- Loop structure : According to whether the conditions are met , Loop executes a piece of code many times .
4.2 Selection structure
4.2.1 if sentence

#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
if (a > b)
{
printf("%d\n", a);
}
return 0;
} 4.2.2 if…else sentence

#include <stdio.h>
int main()
{
int a = 1;
int b = 2;
if (a > b)
{
printf("%d\n", a);
}
else
{
printf("%d\n", b);
}
return 0;
}4.2.3 if…else if…else sentence

#include <stdio.h>
int main()
{
int a;
scanf("%u", &a);
if (a < 10)
{
printf(" bits \n");
}
else if (a < 100)
{
printf(" ten \n");
}
else if (a < 1000)
{
printf(" Hundred bit \n");
}
else
{
printf(" It's big \n");
}
return 0;
}4.2.4 Ternary operator
Grammar format :
Judge the condition ? sentence 1: sentence 2
If the condition is true, execute the statement 1, Otherwise, execute the statement 2.
#include <stdio.h>
int main()
{
int a = 10;
int b = 20;
int c;
c = ( a > b ? a : b );
printf("c2 = %d\n", c);
return 0;
}4.2.5 switch sentence
Grammar format :
switch( The number )
{
case ( Conditions 1):
( Execute statement 1)
break;
case ( Conditions 2):
( Execute statement 2)
break;
.
.
.
default:
( Execute statement n)
}
#include <stdio.h>
int main()
{
char c;
c = getchar();
switch (c) // Parameter can only be integer ( Include character type ) Variable
{
case '1':
printf("OK\n");
break; //switch encounter break It was interrupted , Otherwise, the following statement will continue to be executed
case '2':
printf("not OK\n");
break;
default: // If none of the above conditions are met , Then perform default
printf("are u ok?\n");
}
return 0;
}4.3 Loop structure
4.3.1 while sentence

#include <stdio.h>
int main()
{
int a = 20;
while (a > 10) // If the conditions are met, the cycle will continue
{
scanf("%d", &a);
printf("a = %d\n", a);
}
return 0;
}4.3.2 do…while sentence

#include <stdio.h>
int main()
{
int a = 1;
do
{
a++;
printf("a = %d\n", a);
} while (a < 10); //do...while The loop is to execute first and then judge
return 0;
}4.3.3 for sentence
#include <stdio.h>
int main()
{
int i;
int sum = 0;
for (i = 0; i <= 100; i++) // If the conditions are met, the cycle will continue
{
sum += i;
}
printf("sum = %d\n", sum);
return 0;
}4.3.4 Nested loop
Loop statements can be nested with each other :
#include <stdio.h>
int main()
{
int num = 0;
int i, j, k;
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
for (k = 0; k < 10; k++)
{
printf("hello world\n");
}
}
}
return 0;
}4.4 Jump statements break、continue、goto
4.3.1 break sentence
stay switch Conditional statements and Loop statement You can use break sentence :
- When it appears in switch When in a conditional statement , To terminate a case And jump out of switch structure .
- When it appears in a loop statement , Role is Jump out of the current inner loop statement , Execute the following code .
- When it appears in a nested loop statement , Jump out of the latest inner loop statement , Execute the following code .
#include <stdio.h>
int main()
{
int i = 0;
while (1)
{
i++;
printf("i = %d\n", i);
if (i == 10)
{
break; // Jump out of while loop
}
}
return 0;
}4.3.3 goto sentence ( Jump unconditionally , Use less as far as possible )
#include <stdio.h>
int main()
{
goto End; // Jump unconditionally to End The logo of
printf("aaaaaaaaa\n");
End:
printf("bbbbbbbb\n");
return 0;
}
边栏推荐
- Log desensitization - Reference
- 组件间的相互访问
- Vector容器的系列操作( 详解 )
- C语言力扣第25题之k个一组反转链表。多指针遍历
- 【愚公系列】2022年7月 Go教学课程 012-强制类型转换
- [troubleshooting] common problems and solutions when installing MySQL in Windows system
- MySQL view
- Set the ID field to increase automatically when creating tables in SQL Server (Navicat demo)
- Anaconda与Jupyter Notebook入门级详细使用教程
- OpenCV模板
猜你喜欢

C语言基础篇 —— 2-3 指针与数组

Data Lake (20): Flink is compatible with iceberg, which is currently insufficient, and iceberg is compared with Hudi

Etcd database source code analysis -- etcdserver bootstrap recover store from snapshot

Uniapp warehouse management system source code

Anaconda与Jupyter Notebook入门级详细使用教程
![[paper notes] Research on end positioning of grab manipulator based on multi-sensor data fusion](/img/bb/9db231fe4b01c0f3f91e67143bda0c.png)
[paper notes] Research on end positioning of grab manipulator based on multi-sensor data fusion

ETH的拐点可能指日可待,这就是如何

MySQL view

Simple third-party component log desensitization

Towhee daily model weekly report
随机推荐
[performance optimization methodology series] VI. summary
v-mode
How is MySQL data stored on disk?
L2-029 independent happiness
C语言力扣第25题之k个一组反转链表。多指针遍历
Line Flow Based Simultaneous Localization and Mapping
Google play app store may delete the overview of APP permissions and use a new combination of data security information
【排错必看】Windows系统安装mysql时常见问题及解决方法
Daily model series: July 11, 2022
【网络研究院】机器学习系统的威胁是时候该认真对待了
将视频格式转换为gif图片格式
【论文笔记】基于深度学习的视觉检测及抓取方法
07---布儒斯特角
【C语言】函数知识点总结
Two structures ifconf and ifreq
[hero planet July training leetcode problem solving daily] 17th kuansou
组件间的相互访问
【洛谷】P2357 守墓人
【C语言】自定义类型初阶知识点
Resolve the applicationeventmulticast not initialized - call 'refresh' before multicast events exception