C语言英文课件4:选择结构
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
high
Arithmetic operators
< ,<=, >, >=
low
2020/9/29
==, != Assignment operators
4.2.2 Relational Expression(关系表达式)
1.Concept
Two expressions are connected by a relational operator.
〈expression〉〈relational operator 〉〈expression〉
For example:expressions follow are legal:
a>b
a+b<c-d
(a=wk.baidu.com)<=(b=5)
‘a’>=‘b’
(a>b)= =(b>c)
2.Value of Relational Expression ——logic value “true” —— “1”
Can show the logical execute process of algorithm better
basic shape of N-S flow chart:
S1 block
S2 block is true
condition P
T
F
S1 block S2 block
while P is true S block
(4)(num1<num2)+num3 value=6
2020/9/29
4.3 Logical Operation(逻辑运算) and Logical Expression(逻辑表达式)
Single condition like “x>=0”,is expressed by relational
expression
(1)logical operators:
&&
and
||
or
!
not
2020/9/29
For example:expressions follow are logical expressions (x>=0) && (x<10) ,(x<1) || (x>5) ,! (x= =0),
(year%4==0)&&(year%100!=0)||(year%400==0)
There isn’t logic data in C
value
“false”—— “0”
2020/9/29
For example:suppose num1=3,num2=4, num3=5,well then:
(1)num1>num2 value=0 (2)(num1>num2)!=num3 value=1 (3)num1<num2<num3 value=1
consider:change the value of num1 or num2,the value of the expression will change?
notice:the value of a relational expression,can join in others operations.(why?)
(2)operation rules
1)a&&b:if values of a and b are true,the result is
true,else the result is false.
2)a||b :if values of a and b are false,the result is
Start frame
End frame
I/O frame
operation frame
true
false
condition
distinguishing frame
2020/9/29
connect point
flow line
【example 4.1】algorithm description—traditional flow chart
false,else the result is true.
3) !a :if the value of a is true ,the result is false,else
the result is true.
For example: suppose x=5,
viz. 1
well then (x>=0) && (x<10) value is true,
4.1 Algorithm and Description
•Algroithm:Some restricted steps are taked inorder to solve a special problem.
•features :1. Limited(有穷性) 2. facticity(确定性)3. ≥0 input 4. >0 output 5. validity(有效性)
For example: (year%4==0)&&(year%100!=0)||(year%400==0)
2 .The value of the logical expression logicals,same as relational expression
2020/9/29
numerical value correspond to logicals:
Program list:
main ( ) { float weight,pay; printf(weight= ); scanf(%f , &weight); if (weight<=50) pay=weight*0.35 ; else pay=50*0.35+(weight-50)*0.5; printf( pay=%-7.2f , pay); }
Complex condition like“x>=0”and“x<10”, is expressed by
logical expression
0<=x<10 ×
x>=0 && x<10
4.3.1 Logical Operators and the Precedence
1. Logical Operators and operation rules
A figure describs steps of an algorithm with kinds of prescriptive graphic symbols and lines with arrowhead.
symbols of flow chart:
start
I/O
operation
end
• describing algorithm:natural language,flow chart,pslanguage
[example 4.1]algorithm description——natural language 1.input weight 2.comput value of pay based on weight 3.output value of pay
2020/9/29
(x<-1) || (x>5) value is false,viz. 0
2.The precedence of logical operators (1) ! → && → || (2) !→ arithmetic operators → rational operators → &&→ || → assignment operators 4.3.2 Logical Expression 1.concept a formula,connect one or several relational expressions by logical operators.
Chapter 4 Selective Structure Programming
4.0 Foreword 4.1 Algorithm and Description 4.2 Conditional Operations and Condiyional Expression 4.3 Logical Operations and Logical Expression 4.4 if Statement 4.5 switch Statement 4.6 Examples of Selective Structure Programming
details of step 2 : if weight<=50 则 pay=weight*0.35
else 2020/9/29 pay=50*0.35+(weight-50)*0.5
4.1.1 Program Flow Chart 1.traditional flow chart(程序流程图)
comput pay based on weight’s value
Output pay
second level detailed algorithm
T
weight ≤50
F
pay=weight*0.35 pay=50*0.35+(weight-50)*0.5
2020/9/29
[ex. 4.1] Algorithm description—programming language
start
inputweight
true
false
weight≤50
pay=weight*0.35
pay=50*0.35+(weight-50)*0.5
2020/9/29
outputpay
end
2. N-S structural flow chart
Main feature:cancel flow line,adapt to structural programming,
2020/9/29
4.2 Relational Operation and Its
Expression
4.2.1 Relational Operators(关系运算符) and Precedence
1. Relational Operators
< , <= , >, >= , ==, !=
2. Precedence
(1)sequence (4)until loop
2020/9/29
(2)selective
(3)while loop
S block until P
[example 4.1] algorithm description——N-S flow chart
first level algorithm
read in weight
2020/9/29
4.0 Foreword
【 ex. 4.1】Compute the cost of baggage carried by train.Input the weight of the baggage, compute the cost. standard of cost: (1) lighter then 50 kg(include 50 kg), 0.35 yuan/kg (2) weighter then 50 kg,50 kg like (1),the other excess, 0.50yuan/kg
(1)logicals
numerical value 1----true, 0----false
(2) numerical value true
logicals 0----false, not 0----
For example:num=12, well then: !num 0 num>=1 && num<=31 1
printf(weight= ); scanf(%f , &weight);
if (weight<=50) pay=weight*0.35 ;
else pay=50*0.35+(weight-50)*0.5;
printf( pay=%-7.2f , pay);
}
2020/9/29
basic steps of solving a problem: 0.analysis 1.setting variables 2.design the algorithm 3.writing the program list
analysis: not overweight 0.35*weight cost overweight 0.35*50+0.5*(weight-50)
2020/9/29
1、variables:weight,pay,are all real.
2、program list:
main ( )
{ float weight,pay;