Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.

Similar presentations


Presentation on theme: "Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."— Presentation transcript:

1 Chapter 3 Control Flow Ku-Yaw Chang canseco@mail.dyu.edu.tw Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University

2 2Ku-Yaw ChangControl Flow 3.1 Statements and Blocks An expression such as x = 0 or x = 0 or i++ or i++ or printf(…) printf(…) becomes a statement when it is followed by a semicolon x = 0; x = 0; i++; i++; prinft(...); prinft(...); In C, the semicolon is a statement terminator.

3 3Ku-Yaw ChangControl Flow 3.1 Statements and Blocks Braces { and } are used to group declarations and statements together into a compound statement, or block. Syntactically equivalent to a single statement Syntactically equivalent to a single statement No semicolon after the right brace that ends a block No semicolon after the right brace that ends a block

4 4Ku-Yaw ChangControl Flow 3.2 If-Else The if-else statement is used to express decisions. if (expression) statement 1 else statement 2 The expression is evaluated The expression is evaluated True : expression has a non-zero value False: expression is zero The else part is optional The else part is optional

5 5Ku-Yaw ChangControl Flow 3.2 If-Else Coding shortcuts if (expression) if (expression != 0) Ambiguity when an 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;

6 6Ku-Yaw ChangControl Flow 3.3 Else-If A multi-way decision if (expression) statement else if (expression) statement statement statementelsestatement The last else part Non of the above or default case Can be omitted To catch an impossible condition

7 7Ku-Yaw ChangControl Flow 3.4 Switch A multi-way decision Test whether an expression matches one of a number of constant integer values, and branches accordingly. switch (expression) { case const-expr: statements case const-expr: statements default: statements }

8 8Ku-Yaw ChangControl Flow 3.4 Switch switch (n) { case 1: printf(“The number is 1\n.”); break; case 2: printf(“The number is 2\n.”); break;default: printf(“The number is not 1 or 2\n.”); break;}

9 9Ku-Yaw ChangControl Flow Exercise 3-1 Write a program to count the occurrences of each digits, while space, and all other characters.

10 10Ku-Yaw ChangControl Flow 3.5 Loops – While and For While while (expression) statement The expression is evaluated The expression is evaluated Non-zero: statement is executed and expression is re- evaluated The cycle continues until expression becomes zero The cycle continues until expression becomes zero

11 11Ku-Yaw ChangControl Flow 3.5 Loops – While and For For for (expr 1 ; expr 2 ; expr 3 ) statement is equivalent to expr 1 ; while (expr 2 ) { statement expr 3 ; } Any of the three parts can be omitted Semicolons must remain An infinite loop: for (;;) { … } break or return

12 12Ku-Yaw ChangControl Flow Exercise 3-2 Write a program that calculates and prints the sum of the even integers from 2 to 30.

13 13Ku-Yaw ChangControl Flow Exercise 3-3 Write a function reverse(s) that reverses the string s in place. void reverse(char s[]) void reverse(char s[])

14 14Ku-Yaw ChangControl Flow 3-6 Loops – Do-while The syntax of the do is dostatement while (expression); The body is always executed at least once. The body is always executed at least once. do-while is much less used than while and for. do-while is much less used than while and for.

15 15Ku-Yaw ChangControl Flow 3.6 Loops – Do-while /* itoa: convert n to characters in s */ void itoa(int n, char s[]) { int i, sign; if ((sign=n) < 0)/* record sign */ n = -n;/* make n positive */ n = -n;/* make n positive */ i = 0; do {/* generate digits in reverse order */ s[i++] = n % 10 + ‘0’;/* get next digit */ } while ((n /= 10) > 0);/* delete it */ if (sign < 0)s[i++] = ‘-’; s[i] = ‘\0’; reverse(s);}

16 16Ku-Yaw ChangControl Flow 3.7 Break and Continue The break statement provides an early exit from for, while, and do, just as from switch. A break causes the innermost enclosing loop or switch to be exited immediately.

17 17Ku-Yaw ChangControl Flow 3.7 Break and Continue /* trim: remove trailing blanks, tabs, newlines */ int trim(char s[]) { int n; for (n = strlen(s)-1; n >= 0; n--) if (s[n] != ‘ ‘ && s[n] != ‘\t’ && s[n] != ‘\n’) break; s[n+1] = ‘\0’; return n; }

18 18Ku-Yaw ChangControl Flow 3.7 Break and Continue The continue statement causes the next iteration of the enclosing for, while, or do loop to begin. An example: skip negative values for ( i = 0; i < n; i++ ) { if (a[i] < 0) /* skip negative elements */ continue; continue; … /* do positive elements */ }

19 The End


Download ppt "Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University."

Similar presentations


Ads by Google