Presentation is loading. Please wait.

Presentation is loading. Please wait.

0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April 2007 3.2 if, if … else.

Similar presentations


Presentation on theme: "0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April 2007 3.2 if, if … else."— Presentation transcript:

1 0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April 2007 3.2 if, if … else … 3.3 if … else if … else … 3.4 switch 3.5 Loops: while and for 3.6 Loops: do 3.7 break and continue 3.8 goto and labels

2 1 3.1 Statements and Blocks Statement –An expression such as x=0 or i++ becomes a statement when it is followed by a semicolon, as in : x=0; i++; Compound statement or block –Braces { and } are used to group declarations and statements. –A block is syntactically equivalent to a single statement. Miscellaneous –There is no semicolon after the right brace that ends a block.

3 2 3.2 if [else], 3.3 else if Syntax if (expression) statement Shortcuts if (expression) is equivalent to if (expression != 0) if (expression) statement1 else statement2 if (expression1) statement1 else if (expression2) statement2 else if (expression3) statement3 else statement4 Nested if sequence –Because the else part is optional, there is an ambiguity when the else is omitted from a nested if sequence: if (n > 0) if (a > b) z = a; else z = b; if (n > 0) if (a > b) z = a; else z = b; –This is resolved by associating the else with the closest previous else -less if

4 3 /* binsearch: find x in v[0] <= v[1] <=... <= v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high) / 2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else/* found match */ return mid; } return -1;/* no match */ } An example lowmidhigh v x in v ?

5 4 3.4 Switch switch (expression) { case const-expr1: statements1 case const-expr2: statements2 case const-expr3: statements3 default : statements4 } Example while ((c = getchar()) != EOF) { switch (c) { case ‘0’: printf(“zero\n”); break; case ‘1’: printf(“one\n”); break; default : printf(“other\n”); break; }

6 5 3.5 Loops – while, for Syntax while (expression) statement for (expr1; expr2; expr3) statement Equivalence for (expr1; expr2; expr3) statement is equivalent to expr1; while (expr2) { statement expr3; } Typical use while ((c = getchar()) == ‘ ‘ || c == ‘\n’ || c == ‘\t’) … for (i = 0; i < n; i++) … Infinite loop (sic!) for (;;) ;

7 6 3.6 Loops – do Syntax do statement while (expression) Comma “,” operator for (i = 0, j = strlen(s) - 1; i < j; i++, j--) c = s[i], s[i]= s[j], s[j]= c; Remark A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand.

8 7 3.7 break and continue, 3.8 goto and labels The break statement provides an early exit from for, while, do, and switch. The continue statement causes the next iteration of the enclosed for, while, or do loop. C provides the infinitely-abusable goto statement, and labels to branch to. A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

9 8 Goto’s Typical Use int foo () {... for (... ) {... if (disaster) goto error; }... return... error: /* clean up the mess */ }

10 9 Goto’s Typical Use for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */... found: /* got one: a[i] == b[j] */...

11 10 Goto is not necessary Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes: found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* got one: a[i-1] == b[j-1] */... else /* didn't find any common element */...


Download ppt "0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April 2007 3.2 if, if … else."

Similar presentations


Ads by Google