The while Looping Structure Topics The while Loop
- Slides: 30
The while Looping Structure Topics • The while Loop • Program Versatility • Sentinel Values and Priming Reads • Checking User Input Using a while Loop Reading • Sections 5. 1 – 5. 3, 5. 5 -5. 7 CMSC 104, Version 8/06 L 12 While. Loops. ppt 1
Review: Repetition Structure • A repetition structure allows the programmer to specify that an action is to be repeated while some condition remains true. • There are three repetition structures in C, the while loop, the for loop, and the do-while loop. • Almost all programs use repetition! CMSC 104, Version 8/06 L 12 While. Loops. ppt 2
Similar But Different • Except for one special condition, all three forms of the loops are the same. • Except for the one special condition, any loop can be replaced by either of other two types of loop. • The special condition requires the use of the do-while loop. CMSC 104, Version 8/06 L 12 While. Loops. ppt 3
Will Loops Be Required? 1. Were there any steps I repeated as I solved the problem? If so, which ones? 2. If the answer to question 1 is yes, did I know in advance how many times to repeat the step? 3. If the answer to question 2 is no, how did I know how long to keep repeating the steps? CMSC 104, Version 8/06 L 12 While. Loops. ppt 4
The while Repetition Structure while ( condition ) { statement(s) } The braces are not required if the loop body contains only a single statement. However, they are a good idea and are required by the 104 C Coding Standards. CMSC 104, Version 8/06 L 12 While. Loops. ppt 5
Example while ( children > 0 ) { children = children - 1 ; cookies = cookies * 2 ; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 6
Good Programming Practice • Always place braces around the body of a while loop. • Advantages: o o o Easier to read Will not forget to add the braces if you go back and add a second statement to the loop body Less likely to make a semantic error • Indent the body of a while loop 3 to 5 spaces -- be consistent! CMSC 104, Version 8/06 L 12 While. Loops. ppt 7
Another while Loop Example • Problem: Write a program that calculates the average exam grade for a class of 10 students. • What are the program inputs? o the exam grades • What are the program outputs? o the average exam grade CMSC 104, Version 8/06 L 12 While. Loops. ppt 8
The Pseudocode <total> = 0 <grade_counter> = 1 While (<grade_counter> <= 10) Display “Enter a grade: ” Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / 10 Display “Class average is: “, <average> CMSC 104, Version 8/06 L 12 While. Loops. ppt 9
The C Code 1. #include <stdio. h> 2. int main ( void ) 3. { 4. int counter, grade, total, average ; 5. 6. 7. 8. 9. 10. 11. 12. 13. total = 0 ; counter = 1 ; while ( counter <= 10 ) { printf (“Enter a grade : “) ; scanf (“%d”, &grade) ; total = total + grade ; counter = counter + 1 ; } average = total / 10 ; printf (“Class average is: %dn”, average) ; return 0 ; 14. 15. 16. 17. } CMSC 104, Version 8/06 18. L 12 While. Loops. ppt 10
Versatile? • • How versatile is this program? It only works with class sizes of 10. We would like it to work with any class size. A better way : o Ask the user how many students are in the class. Use that number in the condition of the while loop and when computing the average. CMSC 104, Version 8/06 L 12 While. Loops. ppt 11
New Pseudocode <total> = 0 <grade_counter> = 1 Display “Enter the number of students: “ Read <num_students> While (<grade_counter> <= <num_students>) Display “Enter a grade: ” Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 End_while <average> = <total> / <num_students> Display “Class average is: “, <average> CMSC 104, Version 8/06 L 12 While. Loops. ppt 12
New C Code #include <stdio. h> int main ( ) { int num. Students, counter, grade, total, average ; total = 0 ; counter = 1 ; printf (“Enter the number of students: “) ; scanf (“%d”, &num. Students) ; while ( counter <= num. Students) { printf (“Enter a grade : “) ; scanf (“%d”, &grade) ; total = total + grade ; counter = counter + 1 ; } average = total / num. Students ; printf (“Class average is: %dn”, average) ; return 0 ; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 13
Why Bother to Make It Easier? • Why do we write programs? o So the user can perform some task • The more versatile the program, the more difficult it is to write. BUT it is more useable. • The more complex the task, the more difficult it is to write. But that is often what a user needs. • Always consider the user first. CMSC 104, Version 8/06 L 12 While. Loops. ppt 14
Using a Sentinel Value • We could let the user keep entering grades and when he’s done enter some special value that signals us that he’s done. • This special signal value is called a sentinel value. • We have to make sure that the value we choose as the sentinel isn’t a legal value. For example, we can’t use 0 as the sentinel in our example as it is a legal value for an exam score. CMSC 104, Version 8/06 L 12 While. Loops. ppt 15
The Priming Read • When we use a sentinel value to control a while loop, we have to get the first value from the user before we encounter the loop so that it will be tested and the loop can be entered. • This is known as a priming read. • We have to give significant thought to the initialization of variables, the sentinel value, and getting into the loop. CMSC 104, Version 8/06 L 12 While. Loops. ppt 16
New Pseudocode <total> = 0 <grade_counter> = 1 Display “Enter a grade: “ Read <grade> While ( <grade> != -1 ) <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1 Display “Enter another grade: ” Read <grade> End_while <average> = <total> / <grade_counter> Display “Class average is: “, <average> CMSC 104, Version 8/06 L 12 While. Loops. ppt 17
New C Code #include <stdio. h> int main ( void ) { int counter, grade, total, average ; total = 0 ; counter = 1 ; printf(“Enter a grade: “) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade: “) ; scanf(“%d”, &grade) ; } average = total / counter ; printf (“Class average is: %dn”, average) ; return 0 ; } 104, Version 8/06 CMSC L 12 While. Loops. ppt 18
Final “Clean” C Code #include <stdio. h> int main ( void ) { int counter ; /* counts number of grades entered */ int grade ; /* individual grade */ int total; /* total of all grades */ int average ; /* average grade */ /* Initializations */ total = 0 ; counter = 1 ; CMSC 104, Version 8/06 L 12 While. Loops. ppt 19
Final “Clean” C Code (con’t) /* Get grades from user */ /* Compute grade total and number of grades */ printf(“Enter a grade: “) ; scanf(“%d”, &grade) ; while (grade != -1) { total = total + grade ; counter = counter + 1 ; printf(“Enter another grade: “) ; scanf(“%d”, &grade) ; } /* Compute and display the average grade */ average = total / counter ; printf (“Class average is: %dn”, average) ; return 0 ; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 20
Using A while Loop To Check User Input 1. #include <stdio. h> 2. int main ( void ) 3. { 4. int number ; 5. printf (“Enter a positive integer : “) ; 6. scanf (“%d”, &number) ; 7. 8. 9. 10. 11. 12. 13. 14. 15. } while ( number <= 0 ) { printf (“n. That’s incorrect. Try again. n”) ; printf (“Enter a positive integer: “) ; scanf (“%d”, &number) ; } printf (“You entered: %dn”, number) ; return 0 ; CMSC 104, Version 8/06 L 12 While. Loops. ppt 21
Wrong Way To Check User Input #include <stdio. h> int main ( void ) { int number ; printf (“Enter a positive integer : “) ; scanf (“%d”, &number) ; if ( number <= 0 ) { printf (“n. That’s incorrect. Try again. n”) ; printf (“Enter a positive integer: “) ; scanf (“%d”, &number) ; } printf (“You entered: %dn”, number) ; return 0 ; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 22
Why Is It Wrong? • If you use the if statement do to the error checking, you will force the error to correct it once (and only once), but if the user repeats the same mistake, you let them continue. Usually, the user will try it three or four times before thinking about how to do it correct. The while statement will not let them proceed until they do it correctly! CMSC 104, Version 8/06 L 12 While. Loops. ppt 23
Use #define With Loops #define DAYS_IN_WEEK 7 int nr_days = 1; float total = 0; float sales; while ( nr_days <= DAYS_IN_WEEK ) { printf(“ Enter day %d sales: “ ); scanf( “%f”, &sales ); total = total + sales; nr_days = nr_days + 1; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 24
What Is The Output? #include <stdio. h> int main( void ) { int x = 13; while ( x < 10 ) { printf("x = %dn" ); x = x + 1; } return 0; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 25
What Is The Output? (cont’d) • Nothing! • The reason is the condition was never true, so the body of the loop is never executed. CMSC 104, Version 8/06 L 12 While. Loops. ppt 26
Correct Sentinel Loop 1. Initialize sum to zero 2. Get first score. 3. While score is not the sentinel a. b. Add score to sum Get next score CMSC 104, Version 8/06 L 12 While. Loops. ppt 27
Incorrect Sentinel Loop 1. Initialize sum to zero 2. While score is not the sentinel a. b. • Add score to sum Get next score Why? There is no known value for score to compare to the sentinel, it may or may not be valid. In this case, it is garbage that we do not want to add to the sum. CMSC 104, Version 8/06 L 12 While. Loops. ppt 28
Infinite Loop #include <stdio. h> int main( void ) { int x = 1; while ( x < 10 ) { printf("x = %dn" ); } return 0; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 29
Another Infinite Loop #include <stdio. h> int main( void ) { int x = 1; while ( x != 10 ) { printf("x = %dn" ); x = x + 2; } return 0; } CMSC 104, Version 8/06 L 12 While. Loops. ppt 30
- Statement while... wend digunakan untuk
- Loop aircraft motion
- Control structure
- Perulangan atau pengulangan
- Slidetodoc
- Contoh open loop system
- Fifth gear loop the loop
- Open loop vs closed loop in cars
- Manakah yang lebih baik open loop atau close loop system
- Radial and ulnar loop
- Multi loop pid controller regolatore pid multi loop
- Matlab while loop example
- What are control structures in c++
- Control flow structures
- Mit app inventor while loop
- Causal loop diagram
- Counter control loop
- While loop
- While loop forever
- Do while matlab
- Elixir macros
- Repeat until pascal
- Robotc while loop
- Nested for loop matlab
- Soft model
- Mips while loop
- While loop diagram
- Pengulangan dalam pascal
- Looping writing
- Bentuk umum dari pernyataan perulangan for adalah…
- Looping statement in qbasic