120 likes | 385 Views
Repetition and Loop Statements. Repetition in program The simple loop: go to and if Counting loop and while loop For loop Conditional loop Nested loop Loop design. Example: compute the sum 1 + 2+ 3+ …+100. Algorithm one Sum = 0 Sum = sum+1 Sum = sum +2 …… 101. Sum = sum + 100
E N D
Repetition and Loop Statements • Repetition in program • The simple loop: go to and if • Counting loop and while loop • For loop • Conditional loop • Nested loop • Loop design
Example: compute the sum 1 + 2+ 3+ …+100 • Algorithm one • Sum = 0 • Sum = sum+1 • Sum = sum +2 …… 101. Sum = sum + 100 102. Output Sum This is a bad algorithm • Algorithm two • Sum = 0, I = 1; • If (i<=100)sum = sum = II = i + 1, go to step 2 3. Output Sum implementation #include <stdio.h> main(void) { int sum = 0, i = 1; loop: if (i<=100) {sum = sum + i;i++; /* equivalent to i = i + 1 */goto loop; } printf(“The sum is %d.”, sum); } • Loop body is the statements that are repeated in the loop i <= 100 F T sum=sum+1i=i+1 Output sum
Repetition in Programs • Control structure: a combination of individual instructions into a single logical unit with one entry point and one exit point • Sequence, selection, and repetition are three control structures in most programming language • Repetition is a control structure that repeats a loop body which contains the statements to be repeated • Repetition makes it possible to write a short program which can ran long time • There are four types of loop in C: goto, while, do-while, for Sequence Selection Repetition
While loop • While statement While (loop repetition condition)statement • Example count_star = 0;while (count_star <N) {printf(“*”);count_star = count_star +1;} • Counter-controlled loop (counting loop): a loop whose required number of iterations can be determined before loop execution begins • Loop control variable • Loop repetition condition is the condition that controls loop repetition • Infinite loop
Example: using while loop #include <stdio.h> main(void) { int sum = 0, i = 1; while(i<=100) { sum += i; /* equivalent to sum = sum + i; */ i++; /* equivalent to i += 1 */; } printf(“The sum is %d.”, sum); }
Increment and decrement operators • Increment and decrement variable++; variable += 1; Variable--; variable -= 1; • x = n++; assign the value of n to X, then increase n by 1 • x = ++n; increase n by 1, then assign the value of n to x x = n++; x = --n; Example: Let n = 5. After execution of y = n++; y = ?, n = ? After execution of y = ++n; y = ?, n = ?
Case Study: find investment interests • Input: initial_invest, annual rate, years • Output: the amount, and interests by years • Relation: total amount = initial_investment*(1+rate)year • Algorithm • Get the input: double initial_invest, interest_rate, years • i = 1 • While i <= years amount = amount*(1+rate) Output: year i, amount, amout – investment i = i+1 End while loop
Case study: Find the greatest common divisor • Algorithm • Input two integers u, v • If v equals 0, then gcd = u • Calculate temp = u % v, u = v, v = temp, go to 1. /* Program to find the greatest common divisor */ #include <stdio.h> int main() { int u, v, temp; printf("Please type in two nonnegative integers. \n"); scanf("%i%i", &u, &v); while (v!=0) { temp = u % v; u = v; v = temp; } printf("Their greatest common divisor is %i\n", u); fflush(stdin); getchar(); return 0; }
Do-while loop • Syntaxdo statementwhile (loop repetition condition) • It first execute the statement, then do the check. If the condition is satisfied, then it execute the statement again; otherwise jump out the loop • While loop first check the condition. If it is satisfied, then it execute the statement. Otherwise it jump out. #include <stdio.h> main(void) { int sum = 0, i = 1;do { sum += i; i++; } while(i<=100); printf(“The sum is %d.”, sum); }