The C Programming language Chapter 5 Control Flow Notice ◆If( expression the value may be any type statement be compound statement as #include <stdio. h> 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 <stdio.h> 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 <stdio h main( printf("Enter an integer scanf("%od", &x) if(y<o printf( integer: %/od--->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 <stdio.h> 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); } 例 求一个数的绝对值 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 <stdio.h> 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 <stdio. h> maint char c printf("Enter a character c=getchar if(c<0x20) printf(" The character is a control characterIn") else if(c>=o'&&c<=9') printf("The character is a digit") else if(c>=A'&&c<=Z) printf( "The character is a capital letterin") else if(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 <stdio.h> main() { char c; printf("Enter a character:"); c=getchar(); if(c<0x20) printf("The character is a control character\n"); else if(c>='0'&&c<='9') printf("The character is a digit\n"); else if(c>='A'&&c<='Z') printf("The character is a capital letter\n"); else if(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