LoopsTopicsReview: Repetition StructureThe while Repetition StructureExampleGood Programming PracticeAnother while Loop ExampleThe PseudocodeThe CodeVersatile?New PseudocodeNew CodeWhy Bother to Make It Easier?Using a Sentinel ValueThe Priming ReadSlide 16Slide 17Final "Clean" CodeSlide 19Using a while Loop to Check User InputCounter-Controlled Repetition (Definite Repetition)Event-Controlled Repetition (Indefinite Repetition)Event-Controlled RepetitionThe 3 Parts of a LoopThe for Loop Repetition StructureWhen Does a for Loop Initialize, Test and Modify?A for Loop That Counts From 0 to 9We Can Count Backwards, TooWe Can Count By 2’s ... or 7’s … or WhateverThe do-while Repetition StructureSlide 31An Equivalent while LoopAn Equivalent for LoopSo, Which Type of Loop Should I Use?Nested LoopsNested for LoopsThe break StatementExample break in a for LoopThe continue StatementExample continue in a for Loop1Loops2TopicsThe while LoopProgram VersatilitySentinel Values and Priming ReadsChecking User Input Using a while LoopCounter-Controlled (Definite) RepetitionEvent-Controlled (Indefinite) Repetitionfor Loopsdo-while LoopsChoosing an Appropriate LoopBreak and Continue Statements3Review: Repetition StructureA 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 JavaScript, the while loop, the for loop, and the do-while loop.4The 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 Coding Standards.5Examplewhile ( children > 0 ){ children = children - 1 ; cookies = cookies * 2 ;}6Good Programming PracticeAlways place braces around the body of a while loop.Advantages:Easier to readWill not forget to add the braces if you go back and add a second statement to the loop bodyLess likely to make a semantic errorIndent the body of a while loop 2 to 3 spaces -- be consistent!7Another while Loop ExampleProblem: Write a program that calculates the average exam grade for a class of 10 students.What are the program inputs?the exam gradesWhat are the program outputs?the average exam grade8The Pseudocode<total> = 0<grade_counter> = 1While (<grade_counter> <= 10) Display "Enter a grade: "Read <grade> <total> = <total> + <grade> <grade_counter> = <grade_counter> + 1End_while<average> = <total> / 10Display "Class average is: ", <average>9The Code1. var counter, grade, total, average;2. total = 0;3. counter = 1;4. while (counter <= 10)5. {6. grade = prompt ("Enter a grade : ");7. grade = parseInt(grade);8. total = total + grade;9. counter = counter + 1;10. }11. average = total / 10;12. alert ("Class average is " + average);13.10Versatile?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 :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.11New Pseudocode<total> = 0<grade_counter> = 1Display "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> + 1End_while<average> = <total> / <num_students>Display "Class average is: ", <average>12New Code1. var numStudents, counter, grade, total, average;2. total = 0;3. counter = 1; 4. numStudents = prompt("Enter number of students: ");5. numStudents = parseInt(numStudents);6. while (counter <= numStudents) 7. {8. grade = prompt("Enter a grade : ");9. grade = parseInt(grade);10. total = total + grade;11. counter = counter + 1;12. }13. average = total / numStudents;14. alert ("Class average is: " + average);13Why Bother to Make It Easier?Why do we write programs? So the user can perform some taskThe 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.14Using a Sentinel ValueWe 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.15The Priming ReadWhen 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.16New Pseudocode<total> = 0<grade_counter> = 1Display "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>17New Code1. var counter, grade, total, average;2. total = 0;3. counter = 1;4. grade = prompt("Enter a grade: ");5. grade = parseInt(grade);6. while (grade != -1) 7. {8. total = total + grade;9. counter = counter + 1;10. grade = prompt("Enter another grade: ");11. grade = parseInt(grade);12. }13. average = total / counter;14. alert ("Class average is: " + average);18Final "Clean" Code1. var counter; /* counts number of grades entered */2. var grade; /* individual grade */3. var total; /* total of all grades */4. var average; /* average grade */5. 6. /* Initializations */7. total = 0;8. counter = 1;9. 10. /* Priming read to get initial grade from user */11. grade = prompt("Enter a grade: ");12. grade = parseInt(grade);13. (continued)19Final "Clean" Code17. /* Get grades until user enters -1. Compute 18. grade total and grade count */ 19. while (grade != -1) 20. {21. total = total + grade;22. counter = counter + 1;23. grade = prompt("Enter another grade: ");24. grade = parseInt(grade);25. }26. 27. /*
View Full Document