1 / 12

Repetition and Loop Statements

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

Download Presentation

Repetition and Loop Statements

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. 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

  2. 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

  3. 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

  4. 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

  5. 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); }

  6. 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 = ?

  7. Program Fragment with a Loop

  8. Flowchart for a While Loop

  9. Program to Compute Company Payroll

  10. 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

  11. 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; }

  12. 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); }

More Related