More Loops for loops and dowhile loops CMSC
- Slides: 19
More Loops ‘for’ loops and ‘do-while’ loops CMSC 104 1
Counter-Controlled Repetition with a while loop #include <stdio. h> main () { int i = 1; /* count from 1 to 10 */ while ( i < 11 ) { printf (“%d “, i); i++; } initialization of loop control variable test condition that terminate loop modification of loop control variable } CMSC 104 2
The for loop Repetitive Structure l l The for loop handles details of the counter-controlled loop automatically The initialization of the loop control variable, termination conditional test and modification are handled in for loop structure for ( i = 1; i < 11; i++) { initialization modification } test CMSC 104 3
When does the for loop initialize, test and modify ? l Just as in the while loop that counted, the for loop o Initializes the loop control variable before beginning o modified the loop control variable at the very end of each iteration of the loop o performs the conditional termination test before each iteration of the loop l The for loop is easier to write CMSC 104 4
A for loop that counts from 0 to 9 for (i = 0; i < 10; i++) { printf (“%d”, i); } printf (“n”); CMSC 104 5
We can count backwards, too for (i = 10; i > 0; i--) { printf (“%d”, i); } printf (“n”); CMSC 104 6
We can count by 2’s. . . or 7’s. . . or whatever for (i = 0; i < 10; i += 2) { printf (“%d ”, i); } printf (“n”); CMSC 104 7
The do-while repetitive structure do { statement(s) } while (condition); l CMSC 104 The body of the do-while is ALWAYS executed at least once 8
do-while example do { printf (“Enter a positive number: “); scanf (“%d”, &num); if (num <= 0) { printf (“n. That is not positive, try againn”); } } while (num <= 0); CMSC 104 9
A while that tests input Compare with do-while printf (“Enter a positive number: “); scanf (“%d”, &num); while (num <= 0) { printf (“n. That is not positive, try againn”); printf (“Enter a positive number: “); scanf (“%d”, &num); } CMSC 104 10
for vs while l use a for loop when your program “knows” exactly how many times to loop l use a while loop when there is a condition that will terminate your loop CMSC 104 11
for loops for specified number of iterations printf (“Enter the number of students: “); scanf (“%d”, &num. Students); /* we now “know” how many times to loop */ for (student = 1; student < num. Students; student++) { printf (“Enter grade: “); etc. } CMSC 104 12
use while when a condition terminates your loop l the use of a sentinel is a good example. . . your program doesn’t “know” when the SENTINEL will be encountered printf (“Enter grade: “); scanf (“%d”, &grade); while (grade != SENTINEL) { etc. } CMSC 104 13
while vs do-while l while required “priming read” l do-while required extra test l use do-while when body must be executed at least once CMSC 104 14
break l break can be used in while, do-while and for loops to cause premature exit of the loop. l THIS IS A RECOMMENDED CODING TECHNIQUE CMSC 104 NOT 15
Example break in a loop #include <stdio. h> main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { break; } printf (“%d “, i); } printf (“nbroke out of loop at i = %dn”, i); } CMSC 104 OUTPUT: 1234 Broke out of loop at i = 5 16
continue l continue can be used in a for, while, or do-while loop l It causes the remaining statements in the body of the loop to be skipped for the current iteration of the loop. The loop continues with the next iteration CMSC 104 17
Example of continue in a loop #include <stdio. h> main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { continue; } printf (“%d”, i); } printf (“n”); } CMSC 104 OUTPUT: 12346789 18
Nested for loops CMSC 104 for (i = 1; i < 5; i++) { for (j = 1; j < 3; j++) { if (j % 2 == 0) { printf (“O”); } else { printf (“X”); } } printf (“n”); } How many times is the ‘if’ statement executed? What is the output ? ? 19
- More more more i want more more more more we praise you
- More more more i want more more more more we praise you
- Dowhile
- While do c
- Human history becomes more and more a race
- The more you take the more you leave behind
- The more you study the more you learn
- Aspire not to
- Example of inertia
- Knowing more remembering more
- The more i give to thee the more i have
- More choices more chances
- Iso 22301 utbildning
- Novell typiska drag
- Nationell inriktning för artificiell intelligens
- Ekologiskt fotavtryck
- Varför kallas perioden 1918-1939 för mellankrigstiden
- En lathund för arbete med kontinuitetshantering
- Särskild löneskatt för pensionskostnader
- Personlig tidbok för yrkesförare