当前位置:网站首页>第一部分—C语言基础篇_4. 程序流程结构
第一部分—C语言基础篇_4. 程序流程结构
2022-07-17 10:23:00 【qq_43205256】
4.1 概述
C语言支持最基本的三种程序运行结构:顺序结构、选择结构、循环结构。
- 顺序结构:程序按顺序执行,不发生跳转。
- 选择结构:依据是否满足条件,有选择的执行相应功能。
- 循环结构:依据条件是否满足,循环多次执行某段代码。
4.2 选择结构
4.2.1 if语句

#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语句

#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语句

#include <stdio.h>
int main()
{
int a;
scanf("%u", &a);
if (a < 10)
{
printf("个位\n");
}
else if (a < 100)
{
printf("十位\n");
}
else if (a < 1000)
{
printf("百位\n");
}
else
{
printf("很大\n");
}
return 0;
}4.2.4 三目运算符
语法格式:
判断条件? 语句1: 语句2
条件为真则执行语句1,否则执行语句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语句
语法格式:
switch(数值)
{
case (条件1):
(执行语句1)
break;
case (条件2):
(执行语句2)
break;
.
.
.
default:
(执行语句n)
}
#include <stdio.h>
int main()
{
char c;
c = getchar();
switch (c) //参数只能是整型(包括字符型)变量
{
case '1':
printf("OK\n");
break; //switch遇到break就中断了,否则会继续执行下面的语句
case '2':
printf("not OK\n");
break;
default: //如果上面的条件都不满足,那么执行default
printf("are u ok?\n");
}
return 0;
}4.3 循环结构
4.3.1 while语句

#include <stdio.h>
int main()
{
int a = 20;
while (a > 10) //满足条件则一直循环
{
scanf("%d", &a);
printf("a = %d\n", a);
}
return 0;
}4.3.2 do…while语句

#include <stdio.h>
int main()
{
int a = 1;
do
{
a++;
printf("a = %d\n", a);
} while (a < 10); //do...while循环是先执行后判断
return 0;
}4.3.3 for语句
#include <stdio.h>
int main()
{
int i;
int sum = 0;
for (i = 0; i <= 100; i++) //满足条件则一直循环
{
sum += i;
}
printf("sum = %d\n", sum);
return 0;
}4.3.4 嵌套循环
循环语句之间可以相互嵌套:
#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 跳转语句break、continue、goto
4.3.1 break语句
在switch条件语句和循环语句中都可以使用break语句:
- 当它出现在switch条件语句中时,作用是终止某个case并跳出switch结构。
- 当它出现在循环语句中,作用是跳出当前内循环语句,执行后面的代码。
- 当它出现在嵌套循环语句中,跳出最近的内循环语句,执行后面的代码。
#include <stdio.h>
int main()
{
int i = 0;
while (1)
{
i++;
printf("i = %d\n", i);
if (i == 10)
{
break; //跳出while循环
}
}
return 0;
}4.3.3 goto语句(无条件跳转,尽量少用)
#include <stdio.h>
int main()
{
goto End; //无条件跳转到End的标识
printf("aaaaaaaaa\n");
End:
printf("bbbbbbbb\n");
return 0;
}
边栏推荐
- 多租户 SaaS 的数据库设计模式,你学废了吗?
- Day 7 Training
- SharePoint接入简要笔记
- [Network Research Institute] the threat of machine learning system is time to take it seriously
- 【C语言】 数据类型及意义
- 【C语言】自定义类型初阶知识点
- Questions d'entrevue - concevoir des cas d'essai pour:: memcpy
- Development utility
- 企业数字化转型,为何 SaaS 模式如此重要?
- L2-029 independent happiness
猜你喜欢

【论文笔记】融合多传感器数据的抓取机械臂末端定位研究

【C语言】整形数据的存储

Questions d'entrevue - concevoir des cas d'essai pour:: memcpy

Fundamentals of C language -- 2-1 pointer and wild pointer
![[paper notes] visual detection and capture method based on deep learning](/img/bd/3a204ad3341814cb1ef29b7fc8f324.png)
[paper notes] visual detection and capture method based on deep learning

电脑拨号上网

SAP Fiori 的附件处理(Attachment handling)
![[performance optimization methodology series] VI. summary](/img/46/81261e4ed5c1efe48acb979ac39f7b.jpg)
[performance optimization methodology series] VI. summary

Classificateur knn

【C语言】自定义类型初阶知识点
随机推荐
Day 6 training
实用工具系列 - Xshell安装下载与使用
面试题-给::memcpy函数设计测试用例
LDA classifier
[hero planet July training leetcode problem solving daily] 17th kuansou
Etcd database source code analysis - initialize etcdserver structure
第一部分—C语言基础篇_5. 数组和字符串
idea卡顿且报错:UI was frozen for xxxxx ms问题解决
Go-Excelize API源码阅读(二)——OpenFile()
焱融科技入选北京市 2022 年度“专精特新”,领航混合云文件存储
Vector容器的系列操作( 详解 )
Classificateur knn
Code Capriccio: question skimming record (under update)
LDA分类器
[paper notes] Research on end positioning of grab manipulator based on multi-sensor data fusion
面試題-給::memcpy函數設計測試用例
Un7.16: how to deploy projects on code cloud and invite team members?
el-table 列拖拽(无须引入其他插件)
记忆 lda LDA in blas level-3 SGEMM cublasGemmex cubulasSgemm
C language compilation process