Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programmation impérative - Prof. Béat Hirsbrunner

Similar presentations


Presentation on theme: "Programmation impérative - Prof. Béat Hirsbrunner"— Presentation transcript:

1 Programmation impérative - Prof. Béat Hirsbrunner
Chap. 3 Control Flow 3.1 Statements and Blocks 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 System-oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/sp, Lecture 3 – 8 March 2016 KR – Chap. 3: ~30’; Tutorials: AST for control flow structures: 10'; UNIT Testing: ~25’ + gdb: ~25' +; Exercises: ~90’

2 Programmation impérative - Prof. Béat Hirsbrunner
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 Programmation impérative - Prof. Béat Hirsbrunner
3.2 if [else], else if Syntax if (expression) statement Shortcuts if (expression) is equivalent to if (expression != 0) if (expression) statement1 else statement2 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; This is resolved by associating the else with the closest previous else-less if if (expression1) statement1 else if (expression2) statement2 else if (expression3) statement3 else statement4

4 Programmation impérative - Prof. Béat Hirsbrunner
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; }

5 Programmation impérative - Prof. Béat Hirsbrunner
3.5 Loops – while, for Syntax while (expression) statement for (e1_opt; e2_opt; e3_opt) Equivalence for (expr1; expr2; expr3) statement is equivalent to (except if statement contains a continue statement): expr1; while (expr2) { expr3; } The postfix _opt indicates that the expression is optional Infinite loop (sic!) for (;;) ; Typical use while ((c = getchar()) == ‘ ‘ || c == ‘\n’ || c == ‘\t’) for (i = 0; i < n; i++)

6 Programmation impérative - Prof. Béat Hirsbrunner
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 ones of the last evaluated expression, e.g. in the above example the ones of the expression s[j] = c

7 3.7 break and continue, 3.8 goto and labels
Programmation impérative - Prof. Béat Hirsbrunner été 2002 3.7 break and continue, 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. Note: break and continue operate on the closest enclosing loop or switch 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.

8 Programmation impérative - Prof. Béat Hirsbrunner
Goto’s Typical Use int foo () { ... for ( ... ) { if (disaster) goto error; } return ... error: /* clean up the mess */

9 Programmation impérative - Prof. Béat Hirsbrunner
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] */

10 Programmation impérative - Prof. Béat Hirsbrunner
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 "Programmation impérative - Prof. Béat Hirsbrunner"

Similar presentations


Ads by Google