1 / 30

REpetition

REpetition. Concept of A Loop. Repeating of actions This figure is a loop with no ending We should have a condition to control the loop. Pretest and Post-Test Loops. Pretest loop In each iteration, the loop control expression is tested first

ismail
Download Presentation

REpetition

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

  2. Concept of A Loop • Repeating of actions • This figure is a loop • with no ending • We should have a • condition to control • the loop

  3. Pretest and Post-Test Loops • Pretest loop • In each iteration, the • loop control expression • is tested first • If it is true, the loop • action(s) are executed; • if it is false, the loop • is terminated • Post-test loop • In each iteration, the • loop action(s) are executed. • Then the loop control • expression is tested • If it is true, a new iteration is • started; otherwise, the loop • terminates

  4. Minimum Number of Iterations in Two Loops

  5. Loop Initialization and Updating

  6. Counter-Controlled Loop Concept

  7. C Loop Constructs

  8. The while Statement

  9. Compound while Statement

  10. Process-Control System Example while (1) { temp = getTemperature(); if (temp < 68) turnOnHeater(); else if (temp > 78) turnOnAirCond(); else { turnOffHeater(); turnOffAirCond(); } /* else */ } /* while 1 */

  11. #include <stdio.h> int main (void) { /*Local Definitions */ int num; int lineCount; /*Statements */ printf ("Enter an integer between 1 and 100: "); scanf ("%d", &num); /*Test number */ if (num > 100) num = 100; lineCount = 0; while (num > 0) { if (lineCount < 10) lineCount++; else { printf("\n"); lineCount = 1; } /* else */ printf("%4d", num--); } /* while */ return 0; } /* main */ A while loop to print numbers

  12. Adding a list of numbers #include <stdio.h> int main (void) { /* Local Definitions */ int x; int sum = 0; /* Statements */ printf("Enter your numbers: EOF to stop.\n"); while (scanf("%d", &x) != EOF) sum += x; printf ("\nThe total is: %d\n", sum); return 0; } /* main */ /* EOF is Ctrl+d*/

  13. for Statement

  14. Compound for Statement

  15. Comparing for and while Loops A for loop is used when your loop is to be executed a known number of times. You can do the same thing with a while loop, but the for loop is easier to read and more natural for counting loops

  16. Example of for Loop #include <stdio.h> int main (void) { /*Local Definitions */ int i; int limit; /*Statements */ printf ("\nPlease enter the limit: "); scanf ("%d", &limit); for (i = 1; i <= limit; i++) printf("\t%d\n", i); return 0; } /* main*/

  17. A Simple Nested for Loop #include <stdio.h> int main (void) { /* Local Definitions */ int i; int j; /* Statements */ for (i = 1; i <= 3; i++) { printf("Row %d: ", i); for (j = 1; j<= 5; j++) printf("%3d", j); printf("\n"); } /* for i */ return 0; } /* main */

  18. do…while Statement

  19. Two loops comparing #include <stdio.h> int main (void) { /*Local Definitions */ int loopCount; /*Statements */ loopCount = 5; printf("while loop : "); while (loopCount > 0) printf ("%3d", loopCount--); printf("\n\n"); loopCount = 5; printf("do...while loop: "); do printf ("%3d", loopCount--); while (loopCount > 0); printf("\n"); return 0; } /* main */

  20. #include <stdio.h> int main (void) { /* Local Definitions */ int x; int sum = 0; int testEOF; /* Statements */ printf("Enter your numbers: <EOF> to stop.\n"); do { testEOF = scanf("%d", &x); if (testEOF != EOF) sum += x; } while (testEOF != EOF); printf ("\nTotal: %d\n", sum); return 0; } /* main */ Adding a list with the do…while

  21. The Comma Expression This is a common use of the comma operator for (sum = 0, i = 1; i <=20; i++) { scanf (“%d”, &a); sum += a; } /* for */

  22. Loop Example: Using Nested for Print right triangle flowchart and pseudocode

  23. #include <stdio.h> int main (void) { /*Local Definitions */ int lineCtrl; int numCtrl; int limit; /*Statements */ /* Read limit */ printf("\nPlease enter a number between 1 and 9: "); scanf("%d", &limit); for (lineCtrl = 1; lineCtrl <= limit; lineCtrl++) { for (numCtrl = 1; numCtrl <= lineCtrl; numCtrl++) printf("%1d", numCtrl); printf("\n"); } /* for lineCtrl */ return 0; } /* main */

  24. Jump Statements

  25. break and Inner Loops

  26. continueStatement

  27. continue Example float readAverage (void) { int count = 0; int n; float sum; while(scanf(“%d”,&n) != EOF) { if (n==0) continue; sum += n; count++; } /*end while*/ return (sum/count); } /*readAverage*/ float readAverage (void) { int count = 0; int n; float sum; while(scanf(“%d”,&n) != EOF) { if (n!=0) { sum += n; count++; } /*end if*/ } /*end while*/ return (sum/count); } /*readAverage*/

  28. Example: sumEOF Function int sumEOF (void) { /*Local Definitions */ int nmbr; int sum; /*Statements */ sum = 0; printf ("\nPlease enter an integer: "); while (scanf("%d", &nmbr) != EOF) { sum += nmbr; printf("Next integer <EOF> to stop: "); } /* while */ return sum; } /* sumEOF */

  29. Example: Powers Function int Powers (int base, int exp) { /* Local Definitions */ int i; int result; /* Statements */ if (base < 1 || exp < 0) /* Error Condition */ result = 0; else for (result = 1 , i = 1; i <= exp; i++) result *= base; return result; } /* Powers */

  30. Example: smallestEOF Function int smallestEOF (void) { /* Local Definitions */ int numIn; int smallest; /* Statements */ smallest = INT_MAX; /* requires <limits.h> */ printf("\nPlease enter an integer: "); while (scanf("%d", &numIn) != EOF) { if (numIn < smallest) smallest = numIn; printf("Enter next integer <EOF> to stop: "); } /* while */ return smallest; } /* smallestEOF */

More Related