Download presentation
Presentation is loading. Please wait.
1
1 TDBA66, VT-03, Lecture - Ch. 5 Repetition Loops are common in programming (computers can do boring things over and over again) Type of loopWhen usedC-structures (our tools) CountingWe know how many loops while and for Sentinel-Input of data with a special while and for Controlledending symbol Endfile-controlledInput of data until the file is ended while and for Input validationRepeted input if data is out of range do-while GeneralProcess data until desired condition while and for
2
2 TDBA66, VT-03, Lecture - Ch. 5 while -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 while (expr) statement; next statement;
3
3 TDBA66, VT-03, Lecture - Ch. 5 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; } Ex. 1 and Ex. 2
4
4 TDBA66, VT-03, Lecture - Ch. 5 cnt_char.c
5
5 TDBA66, VT-03, Lecture - Ch. 5 do -statement The condition (expr) is tested after the statement is executed -> the statement is executed at least once do statement; while (expr); next statement;
6
6 TDBA66, VT-03, Lecture - Ch. 5 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 int main(void) { doublesum=0.0, number; intn=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); }
7
7 TDBA66, VT-03, Lecture - Ch. 5 for -satsen Fixed number of loops Semantically the same as expr1; while (expr2) { statement expr3; } next statement for (expr1; expr2; expr3) statement; next statement; for (i=1; i<=5; i=i+1) printf(”2*%d=%d ”,2*i,i); i++
8
8 TDBA66, VT-03, Lecture - Ch. 5 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++;
9
9 TDBA66, VT-03, Lecture - Ch. 5 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 */
10
10 TDBA66, VT-03, Lecture - Ch. 5 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 */
11
11 TDBA66, VT-03, Lecture - Ch. 5 Figure 5.11 Program Showing a Sentinel-Controlled Loop /* Compute the sum of a list of exam scores. */ #include #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); }
12
12 TDBA66, VT-03, Lecture - Ch. 5 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 /* 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); }
13
13 TDBA66, VT-03, Lecture - Ch. 5 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
14
14 TDBA66, VT-03, Lecture - Ch. 5 peppar:~/c/Ckod> cat multplic.c #include 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; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.