Download presentation
Presentation is loading. Please wait.
Published byRuby Higgins Modified over 8 years ago
3
Programming Construct 1 ) We divide the basic programming construct into three types: Sequential Structure 、 Selection Structure and Loop Structure. 2 ) Flow Chart( 流程图 ) : is a graphical representation in which specific symbols and necessary letters are used to describe the steps. 3 ) The flow chart about the three basic programming construct is as follows : A B conditio n A B A Sequential structure Selection Structure
4
1 、 relation operator and relation expression 2 、 logical operators and logical expression 3 、 if statements 4 、 switch statements 5 、 examples Chapter Outline
5
Three forms : ( 1 ) if ( expression ) statements ( 2 ) if ( expression ) statements 1 else statements 2 ( 3 ) if ( expression 1 ) statements 1 else if (expression 2) statements 2 else if (expression 3) statements 3 else if ( expression m ) statements m else statements n ex 第 五 章 选 择 结 构 If 语 句 的 形 式 Brief Summary
6
Forme1 : Forme1 : if ( expression ) statements expression statements F(0) T(not 0) ex : if(x>y) printf(“%d”,x); Firstly,solve the expression. If the value of the expression is not zero , then the corresponding ststement sequence is excuted. If none of the expression is false,then excute the statements after if derectly. 第 五 章 选 择 结 构 If 语 句 的 形 式 example
7
expression statements1statements2 T F ex : if(x>y) printf(“%d”,x); else printf(“%d”,y); Form2 : Form2 : if ( expression ) statements1 else statements2 第 五 章 选 择 结 构 If 语 句 的 形 式 example
8
expression1 expression2 expression3 expression4 statements1 statements2statements3 statements4statements5 F F F F T T T T ex : if (number>500) cost=0.15; else if (number>300) cost=0.10; else if (number>100) cost=0.075; else if (number>50) cost=0.05; else cost=0; Form3 : Form3 : nested- if statements 第 五 章 选 择 结 构 If 语 句 的 形 式 example
9
( 1 ) The expression of three forms behind if- statements always is logical or relation expression. : DCL of three if- statements forms : ( 2 ) In form2 and forme3 , there is a semicolon in front of else, also in the end of all statements. ( 3 ) Behind if or else,it may include only one operate statements,also include many statements,but should be drawed together with brace.({ }) 第 五 章 选 择 结 构 If 语 句 的 形 式
10
Ex: #include main() { int x,y; scanf(“%d,%d”,&x,&y); if(x>y) x=y; y=x; else x++; y++; printf(“%d,%d\n”,x,y); } Compile Error!
11
ex : if(a+b a&&c+a>b) { s=0.5*(a+b+c); area=sqrt(s*(s-a)*(s-b)*(s-c)); printf(“area=%6.sf”,area); } else printf(“it is not a trilateral”); Notice : The brace( “}”)’s outer face need not semicolon 。 Because in the brace({}) there is a full compound statement. 第 五 章 选 择 结 构 If 语 句 的 形 式
12
original program : main() { float a,b,t; scanf(“%f,%f”,&a,&b); if(a>b) { t=a;a=b;b=t; } printf(“%5.2f,%5.2f”,a,b); } ex1 : Input twe real numbers , please output there numbers according to the order of little to big. Need three float variables: a,b,t 第 五 章 选 择 结 构 If 语 句 举 例
13
ex2 : Input three real numbers , please output there numbers according to the order of little to big. original program : main() { float a,b,c,t; scanf(“%f,%f,%f”,&a,&b,&c); if (a>b) {t=a;a=b;b=t;} if (a>c) {t=a;a=c;c=t;} if (b>c) {t=b;b=c;c=t;} printf(“%5.2f,%5.2f,%5.2f”,a,b,c); } 第 五 章 选 择 结 构 If 语 句 举 例 Return
14
#include main() { int a,b; printf("Enter integer a:"); scanf("%d",&a); printf("Enter integer b:"); scanf("%d",&b); if(a==b) printf("a==b\n"); else printf("a!=b\n"); } ex3 : Iput two numbers,decide them to be equally or not. Result : Enter integer a:12 Enter integer b:12 a==b Result : Enter integer a:12 Enter integer b:9 a!=b 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return
15
General form : expression1 ? expression2 : expression3 Condition operator commands three operate objects,it is a operator alonely in C language. ex : max=(a>b) ? a : b; Equal to : if(a>b) max= a; else max=b; 第五章选择结构条件运算符第五章选择结构条件运算符 Condition operator
16
DCL : ( 1 ) Execution order : Firstly,solve the expression 1 , If the value is ture,then solve the expression 2, this moment here is the value of whole expressions. If the value is false, then solve the expression 3, this moment here is the value of whole expressions. ex : max=(a>b) ? a : b; If a=3,b=4; max = 4 ; If a=6 , b=4; max = 6 ; 第五章选择结构条件运算符第五章选择结构条件运算符
17
( 2 ) Condition operator overmatch assigned operator,but under relation and arithmetic operator. ( 3 ) Condition operator’s combined direcion is from right to left. ex1 : max=(a>b) ? a : b; max= a>b ? a : b; ex2 : max=a>b? a : b+1; max= a>b ? a : (b+1); ex3 : max= a>b ? a : c > d ? c:d ; max= a>b ? a : ( c > d ? c:d ) ; Suppose a=1,b=2,c=3,d=4 第五章选择结构条件运算符第五章选择结构条件运算符
18
( 4 ) Condition operator cann’t replce ordinary if-statements,only when the embed statements in it is assigned statements(and two branches assign to the same variable). ex : if (a>b) printf(“%d”,a); else printf(“%d”,b); printf(“%d”,a>b?a:b); ( 5 ) The type of expression 1 and expression 2 can be same,also be different. ex : int x; x ? ‘a’ : ‘b’; ex : x>y ? 1 :1.5 ; 第五章选择结构条件运算符第五章选择结构条件运算符
19
Ex5: Input a letter , iudge it is a capital letter or not , if is , then change it to lower-case letter ; if not , then nothing changed 。 Finally,output this letter 。 main() { char ch; scanf( “ %c “, &ch); ch = ( ch > = 'A‘ && ch < = 'Z‘ ) ? ( ch + 32 ) : ch ; printf( “ %c “, ch ) ; } 第五章选择结构条件运算符第五章选择结构条件运算符 Return
20
ex6 : Function -1 ( x<0) 0 (x=0) 1 (x>0) y= Write a programme , input the value of x. Then ouput y. main( ) { float x ; int y ; scanf(“ %f ”, &x ) ; if ( x < 0 ) y = - 1 ; if ( x = =0 ) y = 0 ; if ( x > 0 ) y = 1 ; printf(“ \n %f, %d ”, x, y ) ; } 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return
21
Algorithm 2 : input x ifx<0 y= -1 else : ifx=0 y=0 ifx>0 y=1 output y Algorithm 3 : input x ifx<0 y=-1 else : ifx=0 y=0 else : y=1 ouput y 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例
22
p1 : main() {float x; int y; scanf(“%f”,&x); if (x<0) y=-1; else if (x==0) y=0; else y=1; printf(“x=%f,y=%d\n”,x,y); } p2 : modify to : if(x>=0) if(x>0) y=1; else y=0; else y=-1; p3 : modify to : y=-1; if(x!=0) if(x>0) y=1; else y=0; p4 : modify to : y=0; if(x>=0) if(x>0) y=1; else y=-1; 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例 Return
23
#include main() { int x,y; printf("Enter an integer:"); scanf("%d",&x); y=x; if(y<0) y= -y; printf("\ninteger:%d--->absolute value:%d\n",x,y); } ex 运行: Enter an integer:-12 integer:-12--->absolute value :12 第 五 章 选 择 结 构 If 语 句 嵌 套 举 例
24
ex #include main() { int x,y; printf("Enter integer x,y:"); scanf("%d,%d",&x,&y); if(x!=y) if(x>y) printf("X>Y\n"); else printf("X<Y\n"); else printf("X==Y\n"); } 运行: Enter integer x,y:12,23 X<Y Enter integer x,y:12,6 X>Y Enter integer x,y:12,12 X==Y 第 五 章 选 择 结 构 If 语 句 举 例
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.