Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Flow C and Data Structures Baojian Hua

Similar presentations


Presentation on theme: "Control Flow C and Data Structures Baojian Hua"— Presentation transcript:

1 Control Flow C and Data Structures Baojian Hua bjhua@ustc.edu.cn

2 Control-flow Control-flow: specifies the order in which the computations are performed C has only primitive forms: branch: to make a choice loops: to repeat Exception, continuation, coroutine?

3 Statement and Block A statement: basic form: expression terminated with a semicolon Ex: x = 9; printf ( “ hello, world\n ” ); A block: is a list of statements: zero, one or more starts with {, ends with } no terminating semicolon

4 If-else The if-else statement expresses decisions. Formally, the syntax is: if (expression) statement1 else statement2 // what will be printed? if (x=17) printf (“in the true branch\n”); else printf (“in the false branch\n”);

5 If Or the else branch could be omitted: if (expression) statement // example. Oooops! if (x=17) printf (“in the true branch\n”);

6 Ambiguity // Ambiguity arises with nested if: if (x<17) if (x<10) printf (“x < 10\n”); else printf (“x >= 17\n”); // however, this program will not behave as we // expect, to see this, try x with initial value // 15. // The reason is that C uses the rule called // “eager matching”

7 To Cure // The correct way is: if (x<17) { if (x<10) printf (“x less than 10\n”); } else printf (“x greater or equal than 17\n”);

8 To Cure // or this: if (x<17) if (x<10) printf (“x less than 10\n”); else ; else printf (“x greater or equal with 17\n”); // In summary, it’s always a good style to: // 1. use {…} in compounded statements; // 2. write explicitly matching “else” branches

9 Else-If // a generalization of if-else is the multiple // branch if statement: if (expression) statement else if (expression) statement else if (expression) statement else if (expression) statement else statement

10 Else-If // Example: given score, return level int calculateLevel (int score) { int level; if (score == 100) level = ‘A’; else if (score == 90) level = ‘B’; else if (c == 80) level = ‘C’; else if …; return level; }

11 Switch // switch behaves very much like the else-if: switch (expression) { case const-expr: statements default: statements } // 1. compute the value v of expression // 2. jump to the appropriate branch according // to v; // 3. jump to the default branch if none is // matched

12 Switch Example int calculateLevel (int score) { int level; switch (score) { case 100: level = ‘A’; break; case 90: level = ‘B’; break; default: …; } return level; }

13 Loops while (expression) statement for (expr1; expr2; expr3) statement // for statement is equivalent to: expr1; while (expr2) { statement expr3; }

14 Break and Continue It is sometimes convenient to be able to exit from a loop directly, other than by testing at the top or bottom. break statement provides an early exit from for, while, or do continue causes the next iteration of the enclosing for, while, or do loop to start

15 Break int a[10]; for (int i=0; i<10; i++) { if (!a[i]) break; …; } …;

16 Continue int a[10]; for (int i = 0; i < 10; i++) { if (a[i] < 0) continue; printf (“%d”, a[i]) }

17 Goto for (…) { if (error) goto handler; } handler: error handling code;

18 Goto A rather weak form of exceptions goto is local (function scope) Unsafe in general Always possible to write code without it So use it rarely We ’ d discuss another (and yet more powerful) form of “ goto ” later


Download ppt "Control Flow C and Data Structures Baojian Hua"

Similar presentations


Ads by Google