23.07.2013 Views

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

JavaScript/JScript: Control Structures I - Pearson Learning Solutions

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

2008934301<br />

Chapter 14 <strong>JavaScript</strong>/<strong>JScript</strong>: <strong>Control</strong> <strong>Structures</strong> I 411<br />

Set total to zero<br />

Set grade counter to one<br />

While grade counter is less than or equal to ten<br />

Input the next grade<br />

Add the grade into the total<br />

Add one to the grade counter<br />

Set the class average to the total divided by ten<br />

Print the class average<br />

Fig. 14.6 Pseudocode algorithm that uses counter-controlled repetition to solve<br />

the class-average problem.<br />

Note the references in the algorithm to a total and a counter. A total is a variable used<br />

to accumulate the sum of a series of values. A counter is a variable used to count—in this<br />

case, to count the number of grades entered. Variables used to store totals should normally<br />

be initialized to zero before being used in a program.<br />

Good Programming Practice 14.5<br />

Variables to be used in calculations should be initialized before their use. 14.5<br />

1 <br />

2 <br />

3 <br />

4<br />

5 <br />

6 Class Average Program<br />

7<br />

8 <br />

9 var total, // sum of grades<br />

10 gradeCounter, // number of grades entered<br />

11 gradeValue, // grade value<br />

12 average, // average of all grades<br />

13 grade; // grade typed by user<br />

14<br />

15 // Initialization Phase<br />

16 total = 0; // clear total<br />

17 gradeCounter = 1; // prepare to loop<br />

18<br />

19 // Processing Phase<br />

20 while ( gradeCounter

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!