Download presentation
Presentation is loading. Please wait.
Published byAubrey Anabel Ford Modified over 9 years ago
1
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops
2
CMSC 1042 #include main () { int i = 1;initialization of loop control variable /* count from 1 to 10 */ while ( i < 11 )test condition that terminate loop { printf (“%d “, i); i++;modification of loop control }variable } Counter-Controlled Repetition with a while loop
3
CMSC 1043 The for loop Repetitive Structure l The for loop handles details of the counter-controlled loop automatically l The initialization of the the loop control variable, termination conditional test and modification are handled in for loop structure for ( i = 1; i < 11; i++) { initializationmodification }test
4
CMSC 1044 When does the for loop initialize, test and modify ? l Just as in the while loop that counted, the for loop oInitializes the loop control variable before beginning omodified the loop control variable at the very end of each iteration of the loop operforms the conditional termination test before each iteration of the loop l The for loop is easier to write
5
CMSC 1045 A for loop that counts from 0 to 9 for (i = 0; i < 10; i++) { printf (“%d”, i); } printf (“\n”);
6
CMSC 1046 We can count backwards, too for (i = 10; i > 0; i--) { printf (“%d”, i); } printf (“\n”);
7
CMSC 1047 We can count by 2’s... or 7’s... or whatever for (i = 0; i < 10; i += 2) { printf (“%d”, i); } printf (“\n”);
8
CMSC 1048 The do-while repetitive structure do { statement(s) } while (condition); l The body of the do-while is ALWAYS executed at least once
9
CMSC 1049 do-while example do { printf (“Enter a positive number: “); scanf (“%d”, &num); if (num <= 0) { printf (“\nThat is not positive, try again\n”); } } while (num <= 0);
10
CMSC 10410 A while that tests input Compare with do-while printf (“Enter a positive number: “); scanf (“%d”, &num); while (num <= 0) { printf (“\nThat is not positive, try again\n”); printf (“Enter a positive number: “); scanf (“%d”, &num); }
11
CMSC 10411 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
12
CMSC 10412 for loops for specified number of iterations printf (“Enter the number of students: “); scanf (“%d”, &numStudents); /* we now “know” how many times to loop */ for (student = 1; student < numStudents; student++) { printf (“Enter grade: “); etc. }
13
CMSC 10413 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. }
14
CMSC 10414 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
15
CMSC 10415 break l break can be used in while, do-while and for loops to cause premature exit of the loop. l THIS IS NOT A RECOMMENDED CODING TECHNIQUE
16
CMSC 10416 Example break in a loop #include main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { break; } printf (“%d “, i); } printf (“\nbroke out of loop at i = %d\n”, i); } OUTPUT: 1 2 3 4 Broke out of loop at i = 5
17
CMSC 10417 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
18
CMSC 10418 Example of continue in a loop #include main ( ) { int i; for (i = 1; i < 10; i++) { if (i == 5) { continue; } printf (“%d”, i); } printf (“\n”); } OUTPUT: 1 2 3 4 6 7 8 9
19
CMSC 10419 Nested for loops 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 ??
20
CMSC 10420 The char data type l The char data type holds a single characterchar 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
21
CMSC 10421 Character Example #include main ( ) { char ch; printf (“Enter a character: “); scanf (“%c”, &ch); printf (“The value of %c is %d.\n”, ch, ch); } If the user entered an A the output would be The value of A is 65.
22
CMSC 10422 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
23
CMSC 10423 getchar () example #include main ( ) { char grade; printf (“Enter a letter grade: “); grade = getchar ( ); printf (“\nThe grade you entered was %c.\n”, grade); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.