1 Flow of Control
2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical Operators and Expressions The Compound Statement The Expression and Empty Statement The if and the if-else Statement
3 Outline (continued) The while Statement The for Statement Example The Comma Operator The do Statement Example: Fibonacci number The goto Statement The break and continue Statements
4 Outline (continued) The switch Statement The Conditional Operators
5 Relational, Equality, and Logical Operators Selection or branch statements are introduced to do alternative actions of computing. The selection is based on true or false Relational, equality, and logical operators are used to navigate the flow of control or structure
6 Relational operatorsLess than< Greater than> Less than or equal to<= Greater than or equal to>= Equality operatorsequal to== not equal to!= logical operators(unary) negation! logical and&& logical or||
7 OperatorsAssociativity ()++(postfix)--(postfix)left to right +(unary) - (unary) ++(prefix)--(prefix)right to left * / %left to right + - left to right >=left to right ==!=left to right &&left to right ||left to right ?:right to left =+=-=*=/=etc right to left,left to right These operators have rules of precedence.
8 Relational Operators and Expressions = Examples a<3 a>b -1.3>=(2.0*x+3.3) a<b<c Not: a=<b a< = b a>>b a-b<0(a-b)<0
9 Relational Operators and Expressions Values of relational expressions a-ba ba = positive0101 zero0011 negative1010
10 Equality Operators and Expressions Demonstrate the use of rules of precedence and associatibvity char c='w''; int i=1, j=2,k=-7; double x=7e+33, y=0.001; 'a'+1<c('a'+1)<c1 -i-5*j>=k+1((-i)-(5*j))>=(k+1)0 3<j<5(3<j)<51 x-3.33<=x+y(x-3.33)<=(x+y)1 x<x+yx<(x+y)0
11 Relational Operators and Expressions == and != are binary operators acting on expressions c=='A' k!=-2 x+y==3*z-7 not: a=b a= = b-1 (x+y) =!44
12 Relational Operators and Expressions expr1-expr2expr1==expr2expr1!=expr2 zero10 nonzero01 example: int i=1,j=2,k=3; i==jj==i0 i!=jj!=i1 i+j+k==-2*-k((i+j)+k)==(-2)*(-k))1
13 Relational Operators and Expressions a!=b!(a==b) a==b and a=b if(a=1) doing nothing … if(a==1) … functioning
14 Logical Operators and Expressions ! is unary operator, and && and || are binary operators !a !(x+7.7) !(a<b||c<d) !!5 not: a! a!=b expr!expr zero (false)1 (true) nonzero (true)0 (false)
15 Logical Operators and Expressions char c='A'; int i=7, j=7; double x=0.0, y=2.3; !c!c0 !(i=-j)!(i=-j)1 !i-j(!i)-j-7 !!(x+y)!(!(x+y))1 !x*!!y(!x)*(!(!y))1
16 Logical Operators and Expressions a&&b a||b !(a<b) &&c 3&&(-2*a+7) not a&& a| |b a&b &b expr1expr2expr1&&expr2expr1||expr2 zerozero00 zerononzero01 nonzerozero01 nonzerononzero11
17 Logical Operators and Expressions expr1expr2expr1&&expr2expr1||expr2 zerozero00 zerononzero01 nonzerozero01 nonzerononzero11
18 Logical Operators and Expressions char c='B'; int i=3,j=3,k=3; double x=0.0,y=2.3; i&&j&&k(i&&j) && k1 x||i&&j-3x||(i&&(j-3))0 i<j&&x<y(i<j)&&(x<y)0 i<j||x<y(i<j)||(x<y)1 'A'<=c&&c<='Z'('A'<=c) &&(c<='Z')1 c-1=='A' ||c+1=='Z'((c-1)=='A')||((c+1)=='Z')1
19 Relational Operators and Expressions short-cut evaluation expr1&&expr2if expr1=0 (false), no need to evaluate expr2 expr1||expr2if expr1 =1 (true), no need to evaluate expr2
20 Relational Operators and Expressions compound statement: series statements are put together inside a brace. It follows scope rule. { a+=b+=c; printf("a=%d, b=%d, c=%d\n", a,b,c); }
21 Relational Operators and Expressions inner and outer blocks, often used in if if, if- else, do-loop, while-loop, and do-while loops { a=1; { b=2; c=3; }
22 The Expression and Empty Statement expression statement is an expression followed by a semicolon (;) empty statement using ; for( ; a<b; ) { if (a<b) a+=2; }
23 The if and the if-else Statement general form if (expr) statement if (grade>=90) printf(Congratulations!\n"); printf("Your grade is %d.\n", grade);
24 The if and the if-else Statement general form if (expr) { statements } if (j<k) { min=j; printf("j is smaller than k\n"); }
25 The if and the if-else Statement general form if (expr) statement1 else statement2 if (x<y) min=x; else min=y;
26 The if and the if-else Statement general form if (expr) { statements (block1) } else { statements (block2) } if (c>'a' &&c<='z') ++lc_cnt; else { ++other_cnt; printf("c is not a lower case letter\n",c); }
27 The if and the if-else Statement else attaches to the nearest if if (a==1) if (b==2) printf("****\n"); else printf("###\n"); if (a==1) if(b==2) printf("****\n"); else printf("###\n"); if (a==1) { if(b==2) { printf("****\n"); } else { printf("###\n"); }
28 The while Statement while statement is introduced to do repetitive action while (expr) statement while (expr) { block } while (i++<n) factorial*=i; while ((c=getchar()!=EOF) { if(c>='a'&& c<='z') ++ lowercase_letter_cnt; ++total_cnt; }
29 The while Statement Make sure there should be at least one exit of the while loop; otherwise, it reaches the endless loop. Example:
30 /* Count blanks, digits, letters, newlines, and others. */ #include int main(void) { int blank_cnt = 0, c, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while ((c = getchar()) != EOF) /* braces not necessary */ if (c == ' ') ++blank_cnt; else if (c >= '0' && c <= '9') ++digit_cnt; else if (c >= 'a' && c = 'A' && c <= 'Z') ++letter_cnt;
31 else if (c == '\n') ++nl_cnt; else ++other_cnt; printf("%10s%10s%10s%10s%10s%10s\n\n", "blanks", "digits", "letters", "lines", "others", "total"); printf("%10d%10d%10d%10d%10d%10d\n\n", blank_cnt, digit_cnt, letter_cnt, nl_cnt, other_cnt, blank_cnt + digit_cnt + letter_cnt + nl_cnt + other_cnt); return 0; } a.out<cnt_char.c blanks digits letters lines others total
32 The for Statement Alternative loop for repetitive action Usually used for accounting expr1 while (expr2) { statement expr3; } for (expr1; expr2; expr3) statement;
33 The for Statement example: for (i=1;i<=n;++i) factorial*i; for (j=2;k%j==0;++j) { printf(%d is a divior of %d\n", j,k); sum+=j; }
34 Example Boolean algebra plays a major role in design of computer circuits. In this algebra all variables have only the values zero or one.Transistors and memory technologies implement zero-one value schemes with currents, voltages, and magnetic orientations. The circuit designer has a function in mind and needs to check whether, for all possible zero-one inputs, the output has the desired behaviors
35 /* Print a table of values for some boolean functions. */ #include int main(void) { int b1, b2, b3, b4, b5; /* boolean variables */ int cnt = 0; /* headings */ printf("\n%5s%5s%5s%5s%5s%5s%7s%7s%11s\n\n", "Cnt", "b1", "b2", "b3", "b4", "b5", "fct1", "fct2", "majority");
36 for (b1 = 0; b1 <= 1; ++b1) for (b2 = 0; b2 <= 1; ++b2) for (b3 = 0; b3 <= 1; ++b3) for (b4 = 0; b4 <= 1; ++b4) for (b5 = 0; b5 <= 1; ++b5) printf("%5d%5d%5d%5d%5d%5d%6d%7d%9d\n", ++cnt, b1, b2, b3, b4, b5, b1 || b3 || b5, b1 && b2 || b4 && b5, b1 + b2 + b3 + b4 + b5 >= 3); putchar('\n'); return 0; }
37 Cnt b1 b2 b3 b4 b5 fct1 fct2 majority
39 The Comma Operator comma operator has the lowest precedence it is a binary operator It associates from left to right int a, b, c; a=0, b=1; for (sum=0,i=1;i<=n; ++i) sum+=i;
40 The Comma Operator int i,j,k=3; double x=3.3; i=1;j=2;++k+1((i=1), (j=2)), ((++k)+1)5 k!=1, ++x*2.0+1(k!=1), ((++x)*2.0)+1)9.6
41 The do Statement a variant of while-loop at least enter the loop once do { statement (block) } while (expr); i=0; sum=0; so { sum+=i; scanf("%d",&I); } while (i>0);
42 /* A test that fails. infinitive loop */ #include int main(void) { int cnt = 0; double sum = 0.0, x; for (x = 0.0; x != 9.9; x += 0.1) { /* trouble! */ sum += x; printf("cnt = %5d\n", ++cnt); } printf("sum = %f\n", sum); return 0; }
43 Example: Fibonacci number sequence of Fibonacci number is defined as f 0 =0f 1 =1f i+1 =f i +f i-1 (for i=1,2,3,…) 0, 1,1,2,3,5,8,13,21,34,55,89,144,233, … Fibonacci number has a lots of interesting properties, for example, Fibonacci quotient defined as q i ={f i }/{f i-1 } for (i=2,3,4,5,… ) converts to the gold mean (1+sqrt(5)/2
44 /* Print Fibonacci numbers and quotients. */ #include #define LIMIT 46 int main(void) { long f0 = 0, f1 = 1, n, temp; /* headings */ printf("%7s%19s%29s\n%7s%19s%29s\n%7s%19s%29s\n", " ", "Fibonacci", "Fibonacci", " n", " number", " quotient", "--", " ", " "); printf("%7d%19d\n%7d%19d\n", 0, 0, 1, 1); /* first two cases */
45 for (n = 2; n <= LIMIT; ++n) { temp = f1; f1 += f0; f0 = temp; printf("%7ld%19ld%29.16f\n", n, f1, (double) f1 / f0); } return 0; }
46 Fibonacci Fibonacci n number quotient
50 The goto Statement goto statement is used to skip to labeled line not suggest to use! examples while (scanf("%lf",&x)==1) { if (x<0.0) goto negative_alert; printf("%f %f\n", sqrt(x), sqrt(2*x)); } negative_alert: printf("Negative value encountered!\n");
51 The break and continue Statements both used to interrupt normal flow of control break causes to exit from the enclosing loop or switch statement continue causes to skip the current operation and go to the next loop
52 The break and continue Statements both used to interrupt normal flow of control break causes to exit from the enclosing loop or switch statement continue causes to skip the current operation and go to the next loop They both are used with a if statement examples
53 The break and continue Statements while(1) { scanf("%lf", &x); if (x<0.0) break; printf("%f\n", sqrt(x); } … for (i=0;i<TOTAL; ++i) { c=getchar(); if (c>='0' && c<='9') continue; /* skip digit */ … }
54 The switch Statement Multiple selection or conditions It can be generated using nested-if-else statements switch© { case ('a'): … break;
55 switch { case ('a'): … break; case ('b'): … break; case ('c'); … break; default: … }
56 The condition Operator ?: Just simplified if-else, but can be put in one statement expr1?expr2:expr3 x=((y<z)?y:z; if (y<z) x=y; else x=z;
57 The condition Operator ?: char a='a', b='b'; int i=1,j=2; double x=7.07; i==j ? a-1: b+1(i==j)?(a-1):(b+1)99 j%3==0?I+4:x((j%3)==0)?(i+4):x7.07 j%3?i+4:x(j%3)?(i+4):x5.0