i The C Programming language Chapter 5 Control Flow 累日工作室# ◆非常感谢天肆掠市建学院 周锦姝老师
The C Programming Language Chapter 5 Control Flow #黑日工作室# 非常感谢 天津城市建设学院 周锦姝老师
i The C Programming language Chapter 5 Control Flow Chapter 5 Control Flow a Overview of c statements a Selected Construction if switch 日 Loops goto and if while and do while or 口 break and continue
The C Programming Language Chapter 5 Control Flow Chapter 5 Control Flow ❑ Overview of C Statements ❑ Selected Construction ▪ if ▪ switch ❑ Loops ▪ goto and if ▪ while and do while ▪ for ❑ break and continue
The C Programming language Chapter 5 Control Flow 5.1 Overview of c statements .C statements: using a semicolon as a statement terminator ☆ Kinds of c statements EXI fye归 Cor as total=total+limit a=3 switch Corhpour func(trents for(tb group dec as i h]l()~ int x=l,y=2, Z statdgrewhile( OT zX+y hcEcontinue printf( %od”,z) break goto return
The C Programming Language Chapter 5 Control Flow 5.1 Overview of C Statements ❖ C statements: using a semicolon “ ;” as a statement terminator ; ❖ Kinds of C statements: • Expression statements:an expression flowed by “;” • Control statements (9): if( )~else~ switch for( )~ while( )~ do~while( ) continue break goto return branches loops others As total=total+limit ; a=3 ; func( ) ; printf(“Hello,world!\n”) ; • Compound statements:using braces {…} to group declarations and statements ,so that they are syntactically equivalent to a single statement. oThere is no “;” after the right brace. As { int x=1,y=2,z ; z=x+y ; printf(“%d”,z) ; }
The C Programming language Chapter 5 Control Flow 5.2 Selected Construction(if and switch) if statement: has three syntax O. if (expression)statement expr flowchart 0 tatement as: if(x>y) printf( %od,, x) ◆·if( expression statement 1 !0 expr else as: if(x>y)max-X ht1 statement2 else max-y
The C Programming Language Chapter 5 Control Flow 5.2 Selected Construction (if and switch) ❖ if statement: has three syntax ▪ if (expression) statement ▪ flowchart expr statement !0 =0 as : if (x>y) printf(“%d”,x); expr statement1 statement2 !0 =0 ▪ if (expression) statement1 else statement2 ▪ flowchart as:if (x>y) max=x ; else max=y ;
i The C Programming language Chapter 5 Control Flow if expr1) statement l else if(expr2) statement2 else if(expr3) Statement as: if(salary> 1000) dex=0. 4 else if(salary>800) index=0.3 else if(salary> 600 index=0.2 else if(salary>400 index=0.1 else index=0 非0 statemnt1 statemnt2 statemnt3 statemntn
The C Programming Language Chapter 5 Control Flow ▪ if ( expr1 ) statement1 else if (expr2 ) statement2 else if (expr3 ) statement3 …... [ else statement n ] expr1 statemnt1 非0 =0 expr2 expr3 statemnt2 statemnt3 statemntn 非0 非0 =0 =0 ▪ flowchart: as: if (salary>1000) index=0.4; else if (salary>800) index=0.3; else if (salary>600) index=0.2; else if (salary>400) index=0.1; else index=0;
The C Programming language Chapter 5 Control Flow Notice ◆If( expression the value may be any type statement be compound statement as #include main( a=b, x-Y) i int,y scanf(%od, %od,, &x, &y) if(x>y) cy, y-X Compile error else X++;y++; printf(%d, %dn”2x2y)
The C Programming Language Chapter 5 Control Flow as: if (a==b&&x==y) printf(“a=b,x=y”); if (3) printf(“OK”); if (‘a’) printf(“%d”,’a’); Notice: ◆ If (expression) ◆ if (x) if (x!=0) if(!x) if(x==0) as: #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! the value may be any type statement may be compound statement
The C Programming language Chapter 5 Control Flow 例求一个数的绝对值 ch5 1.c*/ #include absolute value: %odn"x ,y) Enter an integer:-12 integer: -12--->absolute value: 12
The C Programming Language Chapter 5 Control Flow /*ch5_1.c*/ #include main() { int x,y; printf("Enter an integer:"); scanf("%d",&x); y=x; if(yabsolute value:%d\n",x,y); } 例 求一个数的绝对值 Enter an integer: -12 integer:-12--->absolute value :12
The C Programming language Chapter 5 Control Flow 例输入两个数并判断两数相等否 ch5 2.c*/ #include <stdio h main( Enter integer a: 12 int a b printf("Enter integer a. \ Enter integer b: 12 a==b scan f( 0d",&a) printf( Enter integer b: scanf(%/od", &b) Enter integer a: 12 if(a==b Enter integer b: 9. printf( a==bn") !-=b else printf(al=bn)
The C Programming Language Chapter 5 Control Flow /*ch5_2.c*/ #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"); } 例 输入两个数并判断两数相等否 Enter integer a:12 Enter integer b:12 a= =b Enter integer a:12 Enter integer b:9 a!=b
The C Programming language Chapter 5 Control Flow 例判断输入字符种类 /*ch53.c*/ #include maint char c printf("Enter a character c=getchar if(c=o'&&c=A'&&c=a'&&c<=z) printf( "The character is a lower letterin") else printf("The character is other character n") :: Enter a character:& The character is other character
The C Programming Language Chapter 5 Control Flow /*ch5_3.c*/ #include main() { char c; printf("Enter a character:"); c=getchar(); if(c='0'&&c='A'&&c='a'&&c<='z') printf("The character is a lower letter\n"); else printf("The character is other character\n"); } 例 判断输入字符种类 运行:Enter a character: The character is a control character :8 The character is a digit 运行: Enter a character: D The character is a capital letter 运行: Enter a character: h The character is a lower letter Enter a character:& The character is other character
The C Programming language Chapter 5 Control Flow U if statement can be nested if(expr1) if(expr1) if(expr2) if(expr 2) nested if statement 1 nested if statement 1 else statement2 statement2 if(expr1) if (expr1) statement 1 if (expr 2) statement I nested if else else statement2 if(expr 2) statement2 hested if if(expr 3) statement else statement nested if statement
The C Programming Language Chapter 5 Control Flow ❑ if statement can be nested: if (expr1) if (expr2) statement1 else statement2 else if (expr3) statement3 else statement4 nested if nested if if (expr1) if (expr2) statement1 else statement2 nested if if (expr1) if (expr2) statement1 else statement2 nested if if (expr1) statement1 else if(expr2) statement2 else statement3 nested if