c语言_选择控制结构

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
A. B. C. D. E.
算法的描述方法
用于单分支控制的if语句
用于双分支控制的if-else语句
用于多路选择的switch语句
关系运算符
F.
G.
条件运算符
逻辑运算符
生活中的问题求解:
Problem: 烤蛋糕(Baking a Cake) How to solve:
1. Start
2. 3. 4. 5. 6. 7. 8.
Leabharlann Baidu
printf("max = %d", max);
}
条件运算符和条件表达式
#include <stdio.h> main() { int a, b, max; 表达式1 ? 表达式2 : 表达式3
printf("Input a, b:"); scanf("%d,%d", &a, &b);
if
else
(a > b) max = a;
max = b;
}
max = a > b ? a : b;
printf("max = %d", max);
}
例3-7 输入1个整数,判断该数是奇数还是偶数
读入一个整数 if (该数能被2整除) 则该数为偶数 else 该数为奇数
number % 2 == 0
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); if(number % 2 == 0) { printf("Tne number is even. \n"); } else { printf("Tne number is odd. \n"); } return 0;
输入输出数据
A
B
◦ 标准库函数调用语句 scanf("%d", &pricePerkg); printf("%d", price);
C
Input
• num1
Process
Output
max
• num2
????
选择结构(分支结构) (Selection Structure)
Single Selection
Multiple Selection
Double Selection
If if - else
if - else - if
Relational Operation
Description Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to
单分支(1):计算两整数的最大值
#include <stdio.h> main() { int a, b, max; printf("Input a,b:"); scanf("%d%d", &a, &b); if (a > b) max = a; if (a <= b) max = b; printf("max = %d\n", max); }
Step a expression1
false true true
Step m
expression2
false
Step n
Step x
Step z
# include <stdio.h> int main(void) { double x, y; printf("Enter x:"); scanf("%lf", &x); if (x < 0) { y = 0; } else if (x <= 15) { y = 4 * x / 3; } else { y = 2.5 * x - 10.5; } printf("f(%.2f) = %.2f\n", x, y); return 0; }
Enter a number: 10 The absolute value is 10. Enter a number: -300 The absolute value is 300.
用于双分支控制的条件语句 ( Double Selection)
Pseudocode Structure
Step a if <condition is true> start Step m Step n end_if else start Step x Step y end_else Step z
Enter x: -0.5 f(-0.50) = 0.00 Enter x: 9.5
f(9.50) = 12.67
Step step a
true
condition condition
false
Step step m Step step n
Step step b x
The structure is similar to single selection (flowchart) 表达式非0为真
Syntax: if (expression) statement; or if (expression) { statement1; statement2; }
复合语句
compound statement
被当作一条语句看待
Syntax: if (expression) statement; or if (expression) { statement1; statement2; }
Don’t forget the parentheses !! Don’t forget the braces !!
◦ 没有输入或有多个输入 ◦ 有一个或多个输出
自然语言描述 传统流程图(Flowchart)
在1966年,Bohra 与 Jacopini 提出
N-S结构化流程图
1973年,美国学者I.Nassi 和 B.Shneiderman 提出
伪码(Pseudocode)表示

Flowchart represents algorithm graphically.
#include <stdio.h> main() { int a, b, max;
printf("Input a, b:"); scanf("%d,%d", &a, &b);
if
else
(a > b) max = a;
max = b;
}
if if
(a > b) max = a; (a <= b) max = b;
Input
• quantity • pricePerkg
Process
Output
price
price = quantity * pricePerkg
First identify the input and output of the problem.
给变量赋值
◦ 赋值表达式语句 赋值表达式 ; price = quantity*pricePerkg;
Enter a number: 1028 Tne number is even. Enter a number: 329 Tne number is odd.
}
Multi-way if Step a if (expression1) { Step m } if (expression2) { Step n } Step z
1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.3 冲一杯咖啡 3. End
("Divide and Conquer" Strategy )
1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡 3. End
Step Step a a expression1
false
true
Step Step m m
expression2
false
true
Step Step nn
Step Step z z
Cascaded if Step a if (expression1) { Step m } else if (expression2) { Step n } else { Step x } Step z
Input a,b: 20 15
max = 20 _
输入1个整数,输出它的绝对值 (实现fabs函数)
当number < 0时,number = -number; 当number >= 0时,保持不变
#include <stdio.h> int main(void) { int number; printf("Enter a number: "); scanf("%d", &number); if(number < 0) { number = -number; } printf("The absolute value is %d.\n", number); return 0; }
分治策略

面向过程的程序 = 数据结构 + 算法 计算机中的算法( Algorithm )
◦ 为解决一个具体问题而采取的、确定的、有限的操作 步骤,仅指计算机能执行的算法

◦ 有穷性
◦ 确定性,无歧义 ◦ 有效性
能有效执行 负数开平方 在合理的时间内完成
如果x≥0,则输出Yes;如果x≤0,则输出No
将烤箱预热 准备一个盘子 在盘子上抹上一些黄油 将面粉、鸡蛋、糖和香精混合在一起搅拌均匀 将搅拌好的面粉团放在盘子上 将盘子放到烤箱内 End
Problem: 准备早餐( Prepare a Breakfast)
1. Start 2. 准备早餐 3. End
1. Start 2. 准备早餐 2.1 准备一个金枪鱼三明治 2.2 准备一些薯条 2.3 冲一杯咖啡 3. End
Symbo l Semantic Start/End Process Input/Output Test Connector Flow of activities
Example :买苹果,计算价钱 Calculate and display the price of a number of apples if the quantity in kg and price per kg are given.
Start Input a and b Yes No scanf("%d,%d", &a, &b); if (a > b) max = a; else max = b;
max a
a > b?
max b
Output max End
printf("max = %d\n", max);
双分支(1):计算两整数的最大值
or
if (expression) { statement1; statement2; } else { statement3; statement4; }
计算两整数的最大值
Start Input a and b
max a
Yes
a > b?
No
max b
Output max End
计算两整数的最大值
Examples of Expression 6<9 5 <= 5 2>6 9 >= 5 7 == 5 6 != 5
Value 1 (true) 1 (true) 0 (false) 1 (true) 0 (false) 1 (true)
< <= > >= == !=
Pseudocode Structure step a if <condition is true> start step m step n end_if step b
分治策略
( "Divide and Conquer" Strategy )
1. Start 2.准备早餐 2.1 准备一个金枪鱼三明治 2.1.1 拿来两片面包 2.1.2 准备一些金枪鱼酱 2.2 准备一些薯片 2.2.1 将土豆切成片 2.2.2 油炸这些土豆片 2.3 冲一杯咖啡 2.3.1 烧些开水放入杯中 2.3.2 在水杯中加入一些咖啡和糖 3. End
Step a
true
condition
false
Step m Step n
Step x
Step y
Step z
The structure is similar to double selection (flowchart)
Syntax: if (expression) statement1; else statement2;
相关文档
最新文档