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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

2008934301<br />

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

Here, only the sequence structure has been used—the steps listed are to be executed in order,<br />

one after the other.<br />

Software Engineering Observation 14.7<br />

Each refinement, as well as the top itself, is a complete specification of the algorithm; only<br />

the level of detail varies. 14.7<br />

To proceed to the next level of refinement (the second refinement), we commit to specific<br />

variables. We need a running total of the numbers, a count of how many numbers have<br />

been processed, a variable to receive the string representation of each grade as it is input, a<br />

variable to store the value of the grade after it is converted to an integer and a variable to<br />

hold the calculated average. The pseudocode statement<br />

Initialize variables<br />

may be refined as follows:<br />

Initialize total to zero<br />

Initialize gradeCounter to zero<br />

Notice that only the variables total and gradeCounter are initialized before they are used;<br />

the variables average, grade and gradeValue (for the calculated average, the user input and<br />

the integer representation of the grade, respectively) need not be initialized, because their<br />

values are determined as they are calculated or input.<br />

The pseudocode statement<br />

Input, sum up and count the quiz grades<br />

requires a repetition structure (a loop) that successively inputs each grade. Because we do<br />

not know how many grades are to be processed, we will use sentinel-controlled repetition.<br />

The user at the keyboard will type legitimate grades in one at a time. After the last legitimate<br />

grade is typed, the user will type the sentinel value. The program will test for the sentinel<br />

value after each grade is input and will terminate the loop when the sentinel value is<br />

entered by the user. The second refinement of the preceding pseudocode statement is then<br />

Input the first grade (possibly the sentinel)<br />

While the user has not as yet entered the sentinel<br />

Add this grade into the running total<br />

Add one to the grade counter<br />

Input the next grade (possibly the sentinel)<br />

Notice that in pseudocode, we do not use braces around the pseudocode that forms the body of<br />

the while structure. We simply indent the pseudocode under the while to show that it belongs<br />

to the body of the while. Remember, pseudocode is only an informal program development aid.<br />

The pseudocode statement<br />

Calculate and print the class average<br />

may be refined as follows:<br />

If the counter is not equal to zero<br />

Set the average to the total divided by the counter<br />

Print the average<br />

else<br />

Print “No grades were entered”<br />

e-Business and e-Commerce: How to Program, by Harvey M. Deitel, Paul J. Deitel, and Tem R. Nieto. Published by Prentice Hall.<br />

Copyright © 2001 by <strong>Pearson</strong> Education, Inc.

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

Saved successfully!

Ooh no, something went wrong!