Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) ++ -- !right to left * / % left to right + -left.

Similar presentations


Presentation on theme: "1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) ++ -- !right to left * / % left to right + -left."— Presentation transcript:

1 1 Flow of Control Chapter 4 in ABC

2 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) ++ -- !right to left * / % left to right + -left to right >= left to right == !=left to right &&left to right ||left to right = += -= *= /= etc. right to left A boolean expression evaluates to 0 or 1 (int) There is no type “boolean” in C. Numeric values are used instead: 0  false,  0  true

3 3 int i = 1, j = 2, k = 3; Declarations and Initializations ExpressionEquivalent ExpressionValue i == jj == i0 i != jj != i1 i + j + k == -2 * -k(( i + j ) + k ) == (( -2 ) * ( -k ))1

4 4 char c = `A'; int i = 7, j = 7; double x = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue ! c? ! ( i – j )? ! i – j? ! ! ( x + y )? ! x * ! ! y?

5 5 char c = `A'; int i = 7, j = 7; double x = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue ! c 0 ! ( i – j ) 1 ! i – j( ! i ) – j-7 ! ! ( x + y )! ( ! ( x + y ) )1 ! x * ! ! y( ! x ) * ( ! ( ! y ) )1

6 6 charc = `B'; inti = 3, j = 3, k = 3; doublex = 0.0, y = 2.3; Declarations and Initializations ExpressionEquivalent ExpressionValue 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

7 7 Declarations and Initializations int main(void){ charc = `w‘, c2; inti = 1, j = 2, k = -7; double x = 7e + 33, y = 0.001;... ExpressionEquivalent ExpressionValue ‘a’ + 1 < c? -i – 5 * j >= k + 1? 3 < j < 5? x-3.333 <= x + y? x < x + y? what is the value of c2? c2 is not initialized (i.e. contains garbage). It is NOT a compilation error to access an uninitialized variable

8 8 Declarations and Initializations int main(void){ charc = `w‘, c2; inti = 1, j = 2, k = -7; double x = 7e + 33, y = 0.001;... ExpressionEquivalent ExpressionValue ‘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.333 <= x + y( x – 3.333 ) <= ( x + y )1 x < x + yx < ( x + y )0 c2 is not initialized (i.e. contains garbage). It is NOT a compilation error to access an uninitialized variable

9 9 Short-circuit Evaluation (&&,||) Short circuit evaluation: The evaluation process stops as the outcome “true” / “false” is known. if ( (i != 0) && ( 1/i <....)){... } into cnt = 0; while ( ++cnt <= 3 && (c = getchar()) != EOF ) {. } Keep looping as long as the expression evaluates to a value different than zero Do Something What is the maximum number that getchar() is called? not evaluated if i==0

10 10 if ( a == 1 ) printf( “***\n” ); else printf( “###\n” ); if ( a == 1 ) printf( “***\n” ); printf( “another command\n” ); The if Statement An if / else statement An if statement, not followed by an else statement

11 11 if ( a == 1 ) a single command; else a single command; The if Statement Open a block if you like if ( a == 1 ) { some commands… } else { some commands… }

12 12 Software 1, TAU - 4.7 The if Statement if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); is equivalent to if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); Indentation does not influence the else statement

13 13 Software 1, TAU - 4.7 The if Statement if (a == 1) if (b == 2) printf("***\n"); else printf("###\n"); NOT equivalent to if (a == 1) { if (b == 2) printf("***\n"); } else printf("###\n"); “ Dangling else “ : What about the else???

14 14 Switch Statement switch ( c ) { case `a': ++a_cnt; break; case `b': ++b_cnt; break; case `c': case `C': ++cC_cnt; break; default: ++other_cnt; } value to be evaluated (integral expression) The cases are constant integral expressions with unique values, indicating labels to jump to (according to the value of c)

15 15 The conditional operator exp1?exp2:exp3 x = exp1?exp2:exp3; is Equivalent to: if (exp1) x =exp2; elsex = exp3; Example: chara = `a', b = `b'; inti = 1, j = 2; doublex = 7.07; ExpressionEquivalent ExpressionValueType i == j ? a – 1 : b + 1( i == j ) ? ( a – 1 ) : ( b + 1 )99int j % 3 == 0 ? i + 4 : x(( j % 3 ) == 0 ) ? ( i + 4 ) : x7.07double j % 3 ? i + 4 : x( j % 3 ) ? ( i + 4 ) : x5.0double The type is determined by both exp2 and exp3 – irrespective of which is evaluated ‘ a ‘ has a decimal value of 97

16 16 int i=1, factorial=1; While ( i++ < n ) factorial *= i; While ( ( c = getchar() ) != EOF ) { if ( c >= ‘a’ && c <= ‘z’ ) ++lowercase_letter_cnt; ++total_cnt; } The while Statement Loop until i is equal to, or bigger than n. Loop until the EOF character is encountered.

17 17 for ( expr1; expr2; expr3) statement next statement expr1; while ( expr2 ) { statement expr3 } next statement The while Statement These are equivalent!!!

18 18 The for Statement i = 1;sum = 0; for ( ; i <= 10; ++i)for ( ; i <= 10; ) sum += i; sum += i++; i = 1; sum = 0; for ( ; ; ) { sum += i++; printf("%d\n", sum); } Here we sum the numbers from 1 to 10 Here we have an infinite loop!!! sum = 0 for ( i = 1; i <= 10; ++i ) sum += i; The loop stops when the condition (middle statement) evaluates to 0

19 19 The for Statement for ( sum = 0, i = 1; i <=n; ++i ) sum += i; is equivalent to: for ( sum = 0, i = 1; i <= n; sum += i, ++i ); but is not equivalent to: for ( sum = 0, i = 1; i <= n; ++i, sum += i );

20 20 The comma Operator Usually used in “for loops” - allows for multiple initializations and multiple processing of indices. Has the lowest precedence of all the operators in C left-to-right Associativity The expression exp1,exp2 as a whole has the value and type of the right operand (i.e. exp2) Example: inti, j, k = 3; doublex = 3.3; ExpressionEquivalent ExpressionValue 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

21 21 The do-while Statement int error = 1; do { int, n; printf( “Input a positive integer: ” ); scanf( “%d”, &n ); if (error = (n <= 0) ) printf( “\nERROR; Do it again!\n\n” ); } while (error);

22 22 The do-while Statement int error = 1; do { int res, n; printf( “Input a positive integer: ” ); res = scanf( “%d”, &n ); if (res != 1){ printf(“\nERROR: failed to read integer number!\n\n”); break; } if (error = (n <= 0) ) printf( “\nERROR; Do it again!\n\n” ); } while (error);

23 23 The break Command while(1){ while ( 1 ) { scanf( “%1f”, &x ); if ( x < 0.0 ) break; printf( “%f\n”, sqrt( x ) ); } Exit the loop if x is negative break jumps to here!!

24 24 The continue Command for ( i = 0; i < TOTAL; ++i ) { c = getchar(); if ( c >= `0' && c <= `9' ) continue;. } Jump to the end of the current iteration

25 25 to here goto and labeling goto error;..... error: { printf( “An error has occurred -- bye!\n” ); exit(1); } while (scanf( “%1f”, &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” ); Jump from this line of code to the line with the appropriate label Jump from here

26 26 Count blanks, digits, letters, newlines and others #include int main( void ) { int blank_cnt = 0, c = 0, digit_cnt = 0, letter_cnt = 0, nl_cnt = 0, other_cnt = 0; while ( (c = getchar()) != EOF ) if ( c == ' ' ) ++blank_cnt; else if ( c >= '0' && c <= '9' ) ++digit_cnt; else if (c >= 'a' && c = 'A' && c <= 'Z') ++letter_cnt; else if (c == '\n') ++nl_cnt; else ++other_cnt; Brackets are not neccessary (but it is advised to use them for readability!!)

27 27 Example: Count blanks, digits, letters, newlines and others 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; } blanksdigitsletterslinesotherstotal 1973134827180783

28 28 Example: Print a table of values for some Boolean functions #include int main( void ) { int b1 = 0, b2 = 0, b3 = 0, b4 = 0, b5 = 0; int cnt = 0; printf( “\n%5s%5s%5s%5s%5s%5s%7s%7s%11s\n\n”, “Cnt”, “b1”, “b2”, “b3”, “b4”, “b5”, “fct1”, “fct2”, “majority” );. Boolean variables Headings

29 29 Print a table of values for some Boolean functions 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; } Cntb1b2b3b4b5fct1fct2majority 100000000 200001100 300010000 … The result

30 30 A Test that Fails #include int main( void ) { int cnt = 0; double sum = 0.0, x = 0.0; for ( x = 0.0; x != 9.9; x += 0.1 ) { sum += x; printf( “cnt = %5d\n”, ++cnt ); } printf( “sum = %f\n”, sum ); return 0; } The test x<9.9 is more robust in this case ==> It is a good programming style to use a relational expression, when appropriate, rather than equality expression. The test x<9.9 is more robust in this case ==> It is a good programming style to use a relational expression, when appropriate, rather than equality expression. Trouble!!! (on most machines goes into an infinite loop)

31 31 #include #define LIMIT 46 int main(void) { long f0 = 0, f1 = 1, n = 0, temp = 0; 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 ); 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; } Print Fibonacci Numbers and Quotients Headings The first two cases

32 32 n Fibonacci number Fibonacci quotient 00 11 211.0000000000000000 322.0000000000000000 431.5000000000000000 551.6666666666666667 681.6000000000000000 7131.6250000000000000 ………. 23286571.6180339901755971 ………. 447014087331.6180339887498949 4618363119031.6180339887498949 Print Fibonacci Numbers and Quotients


Download ppt "1 Flow of Control Chapter 4 in ABC. 2 Operators and Associativity OperatorAssociativity +(unary) -(unary) ++ -- !right to left * / % left to right + -left."

Similar presentations


Ads by Google