- 1
- 0
- 约8.75千字
- 约 35页
- 2021-05-16 发布于北京
- 举报
《计算概论A》课程 程序设计部分C 语言的基本成分 —
《计算概论A》课程 程序设计部分
C 语言的基本成分 — 控制成分
李
戈
信息科学技术学院
lige@sei.pku.
研究所
.cn
计算机程序的基本结构什么样的结构才能支持程序运行的逻辑?
计算机程序的基本结构
什么样的结构才能支持程序运行的逻辑?
1966年,G. Jacopini 和 C. Bohm在“Communications of the ACM”上发表论论文“Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules” 。
从理论上证明了:任何具有单入口单出口的程序都可以用三
种基本结构表达:
顺序结构
分支结构
循环结构
C. Bohm G. Jacopini, Flow Diagrams, Turing Machines and Languages with Only Two Formation Rules, Communications of the ACM, vol9(5) May 1966, pp 366-371.
分支语句
分支语句
#includeiostream
#includeiostream using namespace std; int main()
{
float weight = 0, height = 0, healthRate = 0;
cin weightheight;
healthRate = weight / (height*height);
if ((18 = healthRate)(healthRate = 25)) cout 体重适中! endl;
else if ((25 healthRate)(healthRate = 30))
cout 超重!注意控制! endl; else if (( 30 healthRate)(healthRate = 35))
cout 肥胖!减肥吧! endl;
else if ((35 healthRate)(healthRate = 40))
cout 重度肥胖!别吃了! endl;
else
cout 请直接拨打120! endl;
return 0;
}
分支语句
分支语句
#include iostream
#include iostream using namespace std; int main()
{
int year = 0;
cin year;
if (year % 4 == 0)
{
if (year % 100 == 0)
{
if (year % 400 == 0)
cout Y;
else
cout N;
}
else
cout Y;
}
else
cout N;
return 0;
}
if 语句使用须知在执行 if 语句前先对表达式求解if ( )内可以是任意的数值类型(包括整型、实型、字符型、指针型数据)if
if 语句使用须知
在执行 if 语句前先对表达式求解
if ( )内可以是任意的数值类型
(包括整型、实型、字符型、指针型数据)
if (‘a’) cout‘a’endl;
cout“OK”endl;
if (3)
若表达式的值为 0,按“假”处理
若表达式的值为 非0,按“真”处理
多分支语句Switch语句的一般形式如下:当
多分支语句
Switch语句的一般形式如下:
当表达式的值与某一个case后面的常量表达式的值相等时,就执行此case后面的语句,若所有的case中的常量表达式的值都没有与表达式的值匹配的,就执行 ault后面的语句。
switch(表达式)
{
case 常量表达式1: 语句1; case 常量表达式2: 语句2;
… ;
case 常量表达式n: 语句n; ault: 语句n+1;
}
#includeiostream
#includeiostream using namespace std; int main()
{
char grade = a; cingrade; switch (grade)
{
case a: cout 85~100 endl; case b: cout 70~84 endl; case c: cout 60~69 endl; case d: cout <60 endl;
ault: cout error endl;
}
return 0;
}
#includeiostream
#includeiostream using namespace std; int main()
{
char grade = a; cin
您可能关注的文档
最近下载
- 明代宗教信仰与思想控制.docx VIP
- 江苏省南通市2024-2025学年高二上学期期末学业质量监测语文试卷(含答案).pdf VIP
- 国就有成人演出的电影了?想看就要冒生命危险.pdf VIP
- 综合素质练习题及答案.doc VIP
- 2020-2021学年北京高三化学二轮复习 反应原理型简答题(突破二卷)(word版 含答案).docx VIP
- 电力拖动控制线路安装与检修(白银矿冶职业技术学院)知到智慧树答案.docx VIP
- 煤炭巷道掘砌工(高级工)资格考试题库(全真题库).docx VIP
- 影像叙事中情感共鸣生成机制.docx VIP
- 同轴二级圆柱齿轮减速器的设计(硬齿面).doc VIP
- 基于GPU多线程多通道图像高速重构方法、设备及介质.pdf VIP
原创力文档

文档评论(0)