1 flow of control. 2 outline relational, equality, and logical operators relational operators and...

57
1 Flow of Control Flow of Control

Upload: hollie-cook

Post on 31-Dec-2015

230 views

Category:

Documents


2 download

TRANSCRIPT

1

Flow of ControlFlow of Control

2

OutlineOutline

Relational, Equality, and Logical OperatorsRelational Operators and ExpressionsEquality Operators and ExpressionsLogical Operators and ExpressionsThe Compound StatementThe Expression and Empty StatementThe if and the if-else Statement

3

Outline (continued)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)Outline (continued)

The switch StatementThe Conditional Operators

5

Relational, Equality, and Relational, Equality, and Logical OperatorsLogical Operators

Selection or branch statements are introduced to do alternative actions of computing.

The selection is based on true or falseRelational, equality, and logical operators

are used to navigate the flow of control or structure

6

Relational operators Less than <Greater than >Less than or equal to <=Greater than or equal to >=

Equality operators equal to ==not equal to !=

logical operators (unary) negation !logical and &&logical or ||

7

Operators Associativity

() ++(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 Relational Operators and ExpressionsExpressions

< > <= >=Examples

a<3a>b-1.3>=(2.0*x+3.3)a<b<c

Not:a=<ba< = ba>>b

a-b<0 (a-b)<0

9

Relational Operators and Relational Operators and ExpressionsExpressions

Values of relational expressions

a-b a<b a>b a<=b a>=positive 0 1 0 1zero 0 0 1 1negative 1 0 1 0

10

Equality Operators and Equality Operators and ExpressionsExpressions

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)<c 1-i-5*j>=k+1 ((-i)-(5*j))>=(k+1) 03<j<5 (3<j)<5 1x-3.33<=x+y (x-3.33)<=(x+y) 1x<x+y x<(x+y) 0

11

Relational Operators and Relational Operators and ExpressionsExpressions

== and != are binary operators acting on expressions

c=='A'k!=-2x+y==3*z-7

not:a=ba= = b-1(x+y) =!44

12

Relational Operators and Relational Operators and ExpressionsExpressions

expr1-expr2 expr1==expr2 expr1!=expr2

zero 1 0nonzero 0 1

example:int i=1,j=2,k=3;

i==j j==i 0i!=j j!=i 1i+j+k==-2*-k ((i+j)+k)==(-2)*(-k)) 1

13

Relational Operators and Relational Operators and ExpressionsExpressions

a!=b !(a==b)

a==b and a=b

if(a=1) doing nothing …if(a==1) … functioning

14

Logical Operators and Logical Operators and ExpressionsExpressions

! is unary operator, and && and || are binary operators

!a!(x+7.7)!(a<b||c<d)!!5

not:a!a!=b

expr !exprzero (false) 1 (true)nonzero (true) 0 (false)

15

Logical Operators and Logical Operators and ExpressionsExpressions

char c='A';int i=7, j=7;double x=0.0, y=2.3;

!c !c 0!(i=-j) !(i=-j) 1!i-j (!i)-j -7!!(x+y) !(!(x+y)) 1!x*!!y (!x)*(!(!y)) 1

16

Logical Operators and Logical Operators and ExpressionsExpressions

a&&ba||b!(a<b) &&c3&&(-2*a+7)

nota&&a| |ba&b&b

expr1 expr2 expr1&&expr2 expr1||expr2zero zero 0 0zero nonzero 0 1nonzero zero 0 1nonzero nonzero 1 1

17

Logical Operators and Logical Operators and ExpressionsExpressions

expr1 expr2 expr1&&expr2 expr1||expr2zero zero 0 0zero nonzero 0 1nonzero zero 0 1nonzero nonzero 1 1

18

Logical Operators and Logical Operators and ExpressionsExpressions

char c='B';int i=3,j=3,k=3;double x=0.0,y=2.3;

i&&j&&k (i&&j) && k 1x||i&&j-3 x||(i&&(j-3)) 0i<j&&x<y (i<j)&&(x<y) 0i<j||x<y (i<j)||(x<y) 1'A'<=c&&c<='Z' ('A'<=c) &&(c<='Z') 1c-1=='A' ||c+1=='Z' ((c-1)=='A')||((c+1)=='Z') 1

19

Relational Operators and Relational Operators and ExpressionsExpressions

short-cut evaluation

expr1&&expr2 if expr1=0 (false), no need to evaluate expr2

expr1||expr2 if expr1 =1 (true), no need to evaluate expr2

20

Relational Operators and Relational Operators and ExpressionsExpressions

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 Relational Operators and ExpressionsExpressions

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 The Expression and Empty StatementStatement

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 The if and the if-else StatementStatement

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 The if and the if-else StatementStatement

general formif (expr) { statements}

if (j<k){min=j;printf("j is smaller than k\n");}

25

The if and the if-else The if and the if-else StatementStatement

general formif (expr) statement1else statement2

if (x<y) min=x;else min=y;

26

The if and the if-else The if and the if-else StatementStatement

general formif (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 The if and the if-else StatementStatement

else attaches to the nearest ifif (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 StatementThe 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 StatementThe 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 <stdio.h>

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 <= 'z' || 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

208 32 361 28 180 809

32

The for Statement The for Statement

Alternative loop for repetitive actionUsually used for accounting

expr1while (expr2){ statementexpr3;}

for (expr1; expr2; expr3) statement;

33

The for Statement 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

ExampleExample

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 <stdio.h>

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

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

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

The Comma OperatorThe Comma Operator

comma operator has the lowest precedenceit is a binary operatorIt 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 OperatorThe Comma Operator

int i,j,k=3;double x=3.3;

i=1;j=2;++k+1 ((i=1), (j=2)), ((++k)+1) 5k!=1, ++x*2.0+1 (k!=1), ((++x)*2.0)+1) 9.6

41

The do StatementThe do Statement

a variant of while-loopat 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 <stdio.h>

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 numberExample: Fibonacci number

sequence of Fibonacci number is defined as

f0=0 f1=1 fi+1=fi+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 asqi={fi}/{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 <stdio.h>

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

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

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

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

50

The goto StatementThe goto Statement

goto statement is used to skip to labeled linenot 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 The break and continue StatementsStatements

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 The break and continue StatementsStatements

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 statementexamples

53

The break and continue The break and continue StatementsStatements

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 StatementThe switch Statement

Multiple selection or conditionsIt 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 ?: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 ?: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) 99j%3==0?I+4:x ((j%3)==0)?(i+4):x 7.07j%3?i+4:x (j%3)?(i+4):x 5.0