1 / 14

Repetition

Repetition. Loops are common in programming (computers can do boring things over and over again) Type of loop When used C-structures (our tools) Counting We know how many loops while and for Sentinel- Input of data with a special while and for Controlled ending symbol

carney
Télécharger la présentation

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 • Loops are common in programming (computers can do boring things over and over again) Type of loop When used C-structures (our tools) Counting We know how many loops while and for Sentinel- Input of data with a special while and for Controlled ending symbol Endfile-controlled Input of data until the file is ended while and for Input validation Repeted input if data is out of range do-while General Process data until desired condition while and for TDBA66, VT-03, Lecture - Ch. 5

  2. while-statement while (expr) statement; next statement; • Conditional repetition: the condition (expr) is evaluated before the statement (often a compound statement) is either executed or not • If the value of (expr) is true then the statement is executed, otherwise control is tranfered to ”next statement • I.e. the statement inside the while-loop might not be executed • The value of (expr) should be false at some point; otherwise we have created an infinit loop TDBA66, VT-03, Lecture - Ch. 5

  3. Ex. 1 and Ex. 2 factorial = 1.0; i=1; while (i++ < n) factorial *= i; ------------------------------------ lowercase-letter.cnt = 0; total_cnt = 0; while ((c = getchar()) != EOF) { if (c >= ’a’ && c <=’z’) ++lowercase_letter_cnt; ++total_cnt; } TDBA66, VT-03, Lecture - Ch. 5

  4. cnt_char.c TDBA66, VT-03, Lecture - Ch. 5

  5. do-statement do statement; while (expr); next statement; • The condition (expr) is tested after the statement is executed -> the statement is executed at least once TDBA66, VT-03, Lecture - Ch. 5

  6. Compute average Write a program that reads real numbers from the key-board until the value zero is read and computes the average of the read numbers. #include <stdio.h> int main(void) { double sum=0.0, number; int n=0; /* counts the number of values read */ printf(”Type a number of real numbers. End by zero(0.0)\n”); do { scanf(”%lf”, &number); sum = sum +number; n++; } while (number != 0.0); if (n != 0) printf(”\nThe average is %.3f\n”, sum/n); else printf(”\nNo values are typed\n”); return (0); } TDBA66, VT-03, Lecture - Ch. 5

  7. for-satsen for (expr1; expr2; expr3) statement; next statement; • Fixed number of loops Semantically the same as expr1; while (expr2) { statement expr3; } next statement i++ for (i=1; i<=5; i=i+1) printf(”2*%d=%d ”,2*i,i); TDBA66, VT-03, Lecture - Ch. 5

  8. Examples i = 1; sum = 0; for (; i <= 10; ++i) sum += i; Does the same as sum=0; for (i=1; i <= 10; i++) sum = sum +i; As will this code do i = 1; sum = 0; for (; i <= 10; ) sum += i++; TDBA66, VT-03, Lecture - Ch. 5

  9. The comma-operator • Lowest priority of all operators • It is left-right associative • x = (expr1 , expr2); • Means that expr1 is evaluated and than expr2. The value of whole expresion is the value of expr2 • Comma-operator is usually used in for-loops for (sum = 0, i = 1; i <= n; ++i) sum += i; for (sum = 0, i = 1; i <= n; sum += i, ++i); /* Note the empty statement */ for (sum = 0, i = 1; i <= n; ++i, sum += i); /* not same result */ TDBA66, VT-03, Lecture - Ch. 5

  10. break & continue • Changes flow of control • break ends ”the current loop” or switch-statement • continue ends the current iteration and continues with the next iteration while (1) { /* 1 is always true */ scanf(”%lf”, &x); if (x < 0.0) break; printf(”%f\n”, sqrt(x)); } /* break jumps to here */ TDBA66, VT-03, Lecture - Ch. 5

  11. Figure 5.11 Program Showing a Sentinel-Controlled Loop /* Compute the sum of a list of exam scores. */ #include <stdio.h> #define SENTINEL -99 int main(void) { int sum = 0, /* sum of scores input so far */ score; /* current score */ printf("Enter first score (or %d to quit)> ", SENTINEL); for (scanf("%d", &score); score != SENTINEL; scanf("%d", &score)) { sum += score; printf("Enter next score (%d to quit)> ", SENTINEL); } printf("\nSum of exam scores is %d\n", sum); return (0); } TDBA66, VT-03, Lecture - Ch. 5

  12. Figure 5.12 Batch Version of Sum of Exam Scores Program /* * Compute the sum of the list of exam scores stored in the * file scores.dat */ #include <stdio.h> /* defines fopen, fclose, fscanf, fprintf, and EOF */ int main(void) { FILE *inp; /* input file pointer */ int sum = 0, /* sum of scores input so far */ score, /* current score */ input_status; /* status value returned by fscanf */ inp = fopen("scores.dat", "r"); printf("Scores\n"); for (input_status = fscanf(inp, "%d", &score); input_status != EOF; input_status = fscanf(inp, "%d", &score)) { printf("%5d\n", score); sum += score; } printf("\nSum of exam scores is %d\n", sum); fclose(inp); return (0); } TDBA66, VT-03, Lecture - Ch. 5

  13. Nested loops • Inside a loop there might be another loop and an other….. Ex. 1 Write a program the computes and displays a multiplication table for numbers 1 to 12. The output should look like | 1 2 3 4 5 6 7 8 9 10 11 12 -|------------------------------------ 1| 1 2 3 4 5 6 7 8 9 10 11 12 2| 2 4 6 8 10 12 14 16 18 20 22 24 3| 3 6 9 12 15 18 21 24 27 30 33 36 And so on TDBA66, VT-03, Lecture - Ch. 5

  14. peppar:~/c/Ckod> cat multplic.c #include <stdio.h> int main(void){ /* A program that prints the multiplication table for the numbers 1 to 12 */ int i, j; /* Print the heading */ printf("\n |"); for (i=1; i < 13; i++) printf("%3d", i); putchar('\n'); /* underline */ for (i=1; i<40; i++) putchar('-'); putchar('\n'); /* Print the table */ for (i=1; i<=12; i++) { printf("%3d|", i); for (j=1; j<=12; j++) printf("%3d", i*j); putchar('\n'); } /* end of for i-loop */ return 0; } TDBA66, VT-03, Lecture - Ch. 5

More Related