More Loops for loops and dowhile loops CMSC
- Slides: 23
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
The char data type holds a single character char ch; l The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our purpose l Use scanf (“%c”, &ch); to input 1 char l CMSC 104 20
Character Example #include <stdio. h> main ( ) { char ch; printf (“Enter a character: “); scanf (“%c”, &ch); printf (“The value of %c is %d. n”, ch); } If the user entered an A the output would be The value of A is 65. CMSC 104 21
The getchar ( ) function l We can also use the getchar() function that is found in the stdio library l The getchar ( ) function reads one character from stdin and returns that character (value) l The value can then be stored in either a char variable or an integer variable CMSC 104 22
getchar () example #include <stdio. h> main ( ) { char grade; printf (“Enter a letter grade: “); grade = getchar ( ); printf (“n. The grade you entered was %c. n”, grade); } CMSC 104 23
- Lirik lagu more more more we praise you
- More more more i want more more more more we praise you
- Dowhile
- Ciclo do c
- Human history becomes more and more a race
- 5 apples in a basket riddle
- The more you study the more you learn
- Aspire not to have more but to be more
- More inertia
- Knowing more remembering more
- More love to thee o lord
- More choices more chances
- Fspos vägledning för kontinuitetshantering
- Novell typiska drag
- Tack för att ni lyssnade bild
- Returpilarna
- Varför kallas perioden 1918-1939 för mellankrigstiden
- En lathund för arbete med kontinuitetshantering
- Personalliggare bygg undantag
- Tidböcker
- Anatomi organ reproduksi
- Densitet vatten
- Datorkunskap för nybörjare
- Stig kerman