Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical.

Similar presentations


Presentation on theme: "1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical."— Presentation transcript:

1 1 Flow of Control

2 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 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 4 Outline (continued) The switch Statement The Conditional Operators

5 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 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 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 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 9 Relational Operators and Expressions Values of relational expressions a-ba ba = positive0101 zero0011 negative1010

10 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 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 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 13 Relational Operators and Expressions a!=b!(a==b) a==b and a=b if(a=1) doing nothing … if(a==1) … functioning

14 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 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 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 17 Logical Operators and Expressions expr1expr2expr1&&expr2expr1||expr2 zerozero00 zerononzero01 nonzerozero01 nonzerononzero11

18 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 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 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 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 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 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 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 25 The if and the if-else Statement general form if (expr) statement1 else statement2 if (x<y) min=x; else min=y;

26 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 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 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 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 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 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 208 32 361 28 180 809

32 32 The for Statement Alternative loop for repetitive action Usually used for accounting expr1 while (expr2) { statement expr3; } for (expr1; expr2; expr3) statement;

33 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 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 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 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 37 Cnt b1 b2 b3 b4 b5 fct1 fct2 majority 1 0 0 0 0 0 0 0 0 2 0 0 0 0 1 1 0 0 3 0 0 0 1 0 0 0 0 4 0 0 0 1 1 1 1 0 5 0 0 1 0 0 1 0 0 6 0 0 1 0 1 1 0 0 7 0 0 1 1 0 1 0 0 8 0 0 1 1 1 1 1 1 9 0 1 0 0 0 0 0 0 10 0 1 0 0 1 1 0 0 11 0 1 0 1 0 0 0 0 12 0 1 0 1 1 1 1 1 13 0 1 1 0 0 1 0 0 14 0 1 1 0 1 1 0 1 15 0 1 1 1 0 1 0 1

38 38 16 0 1 1 1 1 1 1 1 17 1 0 0 0 0 1 0 0 18 1 0 0 0 1 1 0 0 19 1 0 0 1 0 1 0 0 20 1 0 0 1 1 1 1 1 21 1 0 1 0 0 1 0 0 22 1 0 1 0 1 1 0 1 23 1 0 1 1 0 1 0 1 24 1 0 1 1 1 1 1 1 25 1 1 0 0 0 1 1 0 26 1 1 0 0 1 1 1 1 27 1 1 0 1 0 1 1 1 28 1 1 0 1 1 1 1 1 29 1 1 1 0 0 1 1 1 30 1 1 1 0 1 1 1 1 31 1 1 1 1 0 1 1 1 32 1 1 1 1 1 1 1 1

39 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 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 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 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 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 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 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 46 Fibonacci Fibonacci n number quotient -- --------- --------- 0 0 1 1 2 1 1.0000000000000000 3 2 2.0000000000000000 4 3 1.5000000000000000 5 5 1.6666666666666667 6 8 1.6000000000000001 7 13 1.6250000000000000 8 21 1.6153846153846154 9 34 1.6190476190476191 10 55 1.6176470588235294 11 89 1.6181818181818182 12 144 1.6179775280898876

47 47 13 233 1.6180555555555556 14 377 1.6180257510729614 15 610 1.6180371352785146 16 987 1.6180327868852460 17 1597 1.6180344478216819 18 2584 1.6180338134001253 19 4181 1.6180340557275541 20 6765 1.6180339631667064 21 10946 1.6180339985218033 22 17711 1.6180339850173580 23 28657 1.6180339901755971 24 46368 1.6180339882053250 25 75025 1.6180339889579021 26 121393 1.6180339886704431 27 196418 1.6180339887802426 28 317811 1.6180339887383031

48 48 29 514229 1.6180339887543225 30 832040 1.6180339887482036 31 1346269 1.6180339887505408 32 2178309 1.6180339887496482 33 3524578 1.6180339887499890 34 5702887 1.6180339887498589 35 9227465 1.6180339887499087 36 14930352 1.6180339887498896 37 24157817 1.6180339887498969 38 39088169 1.6180339887498940 39 63245986 1.6180339887498951 40 102334155 1.6180339887498947 41 165580141 1.6180339887498949

49 49 42 267914296 1.6180339887498949 43 433494437 1.6180339887498949 44 701408733 1.6180339887498949 45 1134903170 1.6180339887498949 46 1836311903 1.6180339887498949

50 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 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 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 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 54 The switch Statement Multiple selection or conditions It can be generated using nested-if-else statements switch© { case ('a'): … break;

55 55 switch { case ('a'): … break; case ('b'): … break; case ('c'); … break; default: … }

56 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 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


Download ppt "1 Flow of Control. 2 Outline Relational, Equality, and Logical Operators Relational Operators and Expressions Equality Operators and Expressions Logical."

Similar presentations


Ads by Google