640 likes | 666 Views
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.
E N D
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 CControl Flow Overview
Control Flow • Control flow statements specify the order in which computations are performed. • Sequencing • Conditional • Selection • Looping (or iterative processing)
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
More on Block Statements compound-statement : { declaration-listopt statement-listopt }
Programming Language CTypes, Operators and Expressions If-Else Statement
If-Else Statement if (expression) statement1 else statement2
If-Else Statement If expression is evaluated to nonzero, statement1 is executed; otherwise, statement2 is executed. if (expression) statement1 else statement2 expression option
Shortcut if(expression != 0) ... if(expression) ...
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
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; }
Programming Language CTypes, Operators and Expressions Else-If Statement
Else-If Statement if (expression) statement else if (expression) statement else if (expression) statement else if (expression) statement else statement option
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)
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 =
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
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
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
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
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 =
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
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
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
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
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
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
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
-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
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 */ }
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
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.
Programming Language CTypes, Operators and Expressions Switch Statement
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
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; }
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.
Programming Language CTypes, Operators and Expressions Loops – While and For
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
for Statement Continuing check. Loop variable(s) update Loop variable(s) initialization for (expr1; expr2; expr3) statement
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
any of them can be omitted While vs. For while (expression) statement for (expr1; expr2; expr3) statement
Infinite Loops for(; ;){ } while(1){ } while (expression) statement for (expr1; expr2; expr3) statement
. . . . . . . . . . . . . . . . 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
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; } }
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; } }
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.
Programming Language CTypes, Operators and Expressions Do-While Statement