1 / 64

Programming Language  C Control Flow

Programming Language  C Control Flow. 主講人:虞台文. Content. Overview If-Else Statement Else-If Statement Switch Statement Loops - While and For Do-While Statement Break and Continue Goto and Labels. Programming Language  C Control Flow. Overview. Control Flow.

Download Presentation

Programming Language  C Control Flow

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Programming Language  CControl Flow 主講人:虞台文

  2. Content • Overview • If-Else Statement • Else-If Statement • Switch Statement • Loops - While and For • Do-While Statement • Break and Continue • Goto and Labels

  3. Programming Language  CControl Flow Overview

  4. Control Flow • Control flow statements specify the order in which computations are performed. • Sequencing • Conditional • Selection • Looping (or iterative processing)

  5. Statements and Blocks • Simple Statements lower = 0; upper = 300; step = 20; fahr = lower; • Null Statement ; // a null statement • Compound Statements (block statements) { celsius = (5.0/9.0) * (fahr-32.0); printf("%3.0f %6.1f\n", fahr, celsius); fahr = fahr + step; } 4 simple statements 1 compound statement

  6. More on Block Statements compound-statement : { declaration-listopt statement-listopt }

  7. Programming Language  CTypes, Operators and Expressions If-Else Statement

  8. If-Else Statement if (expression) statement1 else statement2

  9. If-Else Statement If expression is evaluated to nonzero, statement1 is executed; otherwise, statement2 is executed. if (expression) statement1 else statement2 expression option

  10. Shortcut if(expression != 0) ... if(expression) ...

  11. Dangling Else Problem n=5; a=2; b=3; z=20; if (n > 0) if (a > b) z = a; else z = b; if (n > 0) if (a > b) z = a; else z = b; z = ? z = 20 z = 3

  12. Ambiguity Removal if (n > 0){ if (a > b) z = a; } else z = b; if (n > 0) if (a > b) z = a; else z = b; if (n > 0){ if (a > b) z = a; else z = b; }

  13. Programming Language  CTypes, Operators and Expressions Else-If Statement

  14. Else-If Statement if (expression) statement else if (expression) statement else if (expression) statement else if (expression) statement else statement option

  15. v[] 0 1 1 5 2 9 3 25 4 80 5 125 6 137 7 140 8 180 9 201 10 400 Example: Binary Search int binsearch(int x, int v[], int n); 6  binsearch(137, v, 11) 9  binsearch(201, v, 11) -1  binsearch(45, v, 11)

  16. v[] 0 1 1 5 2 9 3 25 4 80 5 125 6 137 7 140 8 180 9 201 10 400 binsearch(25, v, 11) Example: Binary Search low <= high mid = (low + high) / 2 = 5 low = > 25 v[mid] = 125 mid = high =

  17. binsearch(25, v, 11) Example: Binary Search v[] low = 0 1 1 5 2 9 > 25 v[mid] = 125 3 25 4 high = 80 high = mid - 1 5 mid = 125 6 137 7 140 8 180 9 201 10 400

  18. binsearch(25, v, 11) Example: Binary Search low <= high v[] mid = (low + high) / 2 = 2 low = 0 1 1 5 2 mid = 9 < 25 v[mid] = 9 3 25 4 high = 80 5 125 6 137 7 140 8 180 9 201 10 400

  19. binsearch(25, v, 11) Example: Binary Search v[] 0 1 1 5 2 mid = 9 < 25 v[mid] = 9 low = 3 25 4 high = 80 low = mid + 1 5 125 6 137 7 140 8 180 9 201 10 400

  20. 3  binsearch(25, v, 11) Example: Binary Search low <= high v[] mid = (low + high) / 2 = 3 0 1 1 5 2 9 == 25 v[mid] = 25 mid = low = 3 25 4 high = 80 5 125 6 137 7 140 8 180 9 201 10 400

  21. v[] 0 1 1 5 2 9 3 25 4 80 5 125 6 137 7 140 8 180 9 201 10 400 binsearch(45, v, 11) Example: Binary Search low <= high mid = (low + high) / 2 = 5 low = > 25 v[mid] = 125 mid = high =

  22. binsearch(45, v, 11) Example: Binary Search v[] low = 0 1 1 5 2 9 > 45 v[mid] = 125 3 25 4 high = 80 high = mid - 1 5 mid = 125 6 137 7 140 8 180 9 201 10 400

  23. binsearch(45, v, 11) Example: Binary Search low <= high v[] mid = (low + high) / 2 = 2 low = 0 1 1 5 2 mid = 9 < 45 v[mid] = 9 3 25 4 high = 80 5 125 6 137 7 140 8 180 9 201 10 400

  24. binsearch(45, v, 11) Example: Binary Search v[] 0 1 1 5 2 mid = 9 < 45 v[mid] = 9 low = 3 25 4 high = 80 low = mid + 1 5 125 6 137 7 140 8 180 9 201 10 400

  25. binsearch(45, v, 11) Example: Binary Search low <= high v[] mid = (low + high) / 2 = 3 0 1 1 5 2 9 < 45 v[mid] = 25 mid = low = 3 25 4 high = 80 5 125 6 137 7 140 8 180 9 201 10 400

  26. binsearch(45, v, 11) Example: Binary Search v[] 0 1 1 5 2 9 < 45 v[mid] = 25 mid = 3 25 low= 4 high = 80 low = mid + 1 5 125 6 137 7 140 8 180 9 201 10 400

  27. binsearch(45, v, 11) Example: Binary Search low <= high v[] mid = (low + high) / 2 = 4 0 1 1 5 2 9 > 45 v[mid] = 80 3 25 low= 4 high = 80 mid = 5 125 6 137 7 140 8 180 9 201 10 400

  28. binsearch(45, v, 11) Example: Binary Search v[] 0 1 1 5 2 9 > 45 v[mid] = 80 3 high = 25 low= 4 80 high = mid - 1 mid = 5 125 6 137 7 140 8 180 9 201 10 400

  29. -1  binsearch(45, v, 11) Example: Binary Search  low <= high v[] 0 1 1 5 2 9 3 high = 25 low= 4 80 mid = 5 125 6 137 7 140 8 180 9 201 10 400

  30. Example: Binary Search /* 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 */ }

  31. Example: Binary Search

  32. Exercises • Consider the following grading table: Write a program that reads students’ grades and prints out the corresponding class for each grade. Grade Class 100-90 A 89-80 B 79-70 C 69-60 D <60 F

  33. Exercises • Write a program to compute by bisection the square root of a positive integer N given by the user. The algorithm proceeds as follows: Start with N and 1 as the upper and lower bounds, and then find the midpoint of the two bounds. If the square of the midpoint is equal or very close to N, then output the midpoint as the answer and the algorithm terminates. If the square of the midpoint is greater than N, then let the upper bound be the midpoint; otherwise let the lower bound be the midpoint. Repeat the process until the two most recent estimates of the square root of N is within 0.000005 of each other. Output the last estimate as the answer and the algorithm terminates.

  34. Programming Language  CTypes, Operators and Expressions Switch Statement

  35. Switch Statement switch (expression) { case item1: statement1; break; case item2: statement2; break; . . . . . . . . case itemn: statementn; break; default: statement; break; } Execution falls through if without break. option

  36. Example switch (letter) { case 'A': numberofvowels++; break; case 'E': numberofvowels++; break; case 'I': numberofvowels++; break; case 'O': numberofvowels++; break; case 'U': numberofvowels++; break; case ' ': numberofspaces++; break; default: numberofconsonants++; break; } switch (letter) { case 'A': case 'E': case 'I': case 'O': case 'U': numberofvowels++; break; case ' ': numberofspaces++; break; default: numberofconsonants++; break; } 

  37. Exercise • Write a function escape(s, t) that converts characters like newline and tab into visible escape sequences like \n and \t as it copies the string t to s. Use a switch. Write a function for the other direction as well, converting escape sequences into the real characters.

  38. Programming Language  CTypes, Operators and Expressions Loops – While and For

  39. While Statement The statement is cyclically executed as long as the expression is evaluated non-zero. This cycle continues untilexpression becomes zero, at which point execution resumes afterstatement. while (expression) statement

  40. for Statement Continuing check. Loop variable(s) update Loop variable(s) initialization for (expr1; expr2; expr3) statement

  41. While vs. For • The choice between while and for is arbitrary, based on which seems clearer • The for is usually used for a counter-like loop expr1; while (expr2) { statement expr3; }  while (expression) statement for (expr1; expr2; expr3) statement

  42. any of them can be omitted While vs. For while (expression) statement for (expr1; expr2; expr3) statement

  43. Infinite Loops for(; ;){ } while(1){ } while (expression) statement for (expr1; expr2; expr3) statement

  44. . . . . . . . . . . . . . . . . if(…) break; or if(…) return; if(…) break; or if(…) return; . . . . . . . . . . . . . . . . How to break the loop? Infinite Loops for(; ; ;){ } while(1){ } while (expression) statement for (expr1; expr2; expr3) statement

  45. Example: Bubble Sort

  46. Example: Bubble Sort

  47. Example: Bubble Sort (I) void BubbleSort(int data[], int n) { int tmp, i, j; for(i=0; i<n-1; i++) for(j=0; j<n-i-1; j++) if(data[j] > data[j+1]){ tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; } }

  48. Example: Bubble Sort (II) void BubbleSort(int data[], int n) { int tmp, i, j, sorted; for(i=0, sorted=FALSE; !sorted&& i<n-1; i++) for(j=0, sorted=TRUE; j<n-i-1; j++) if(data[j] > data[j+1]){ tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; sorted = FALSE; } } void BubbleSort(int data[], int n) { int tmp, i, j; for(i=0; i<n-1; i++) for(j=0; j<n-i-1; j++) if(data[j] > data[j+1]){ tmp = data[j]; data[j] = data[j+1]; data[j+1] = tmp; } }

  49. Exercises • Modify the BubbleSort function such that its outer loop is a while statement. Verify your result. • Modify the BubbleSort function such that its all loops are a while statement. Verify your result.

  50. Programming Language  CTypes, Operators and Expressions Do-While Statement

More Related