310 likes | 639 Vues
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
E N D
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 • 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
Process-Control System Example while (1) { temp = getTemperature(); if (temp < 68) turnOnHeater(); else if (temp > 78) turnOnAirCond(); else { turnOffHeater(); turnOffAirCond(); } /* else */ } /* while 1 */
#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
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*/
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
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*/
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 */
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 */
#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
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 */
Loop Example: Using Nested for Print right triangle flowchart and pseudocode
#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 */
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*/
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 */
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 */
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 */