Page 81 - Introduction to Programming with Java: A Problem Solving Approach
P. 81
Formal Pseudocode
The following Bowling Scores algorithm uses a more formal pseudocode:
totalScore ← 0
count ← 0
print "Enter score (1 to quit): " input score
while (score ≠ 1)
{
totalScore ← totalScore score count ← count 1
print "Enter score (1 to quit): " input score
}
avg ← totalScore / count
print "Average score is " avg
2.12 Other Pseudocode Formats and Applications 47
This formal variation of pseudocode uses special symbols to make operations stand out more dramati-
cally. The left-pointing arrow (←) represents the right-to-left assignment illustrated previously in Figure 2.3.
The ≠ says “is not equal to” more succinctly than words say it. The curly braces emphasize the subordinate
nature of the statements in the body of the while loop. Later you’ll see that Java requires such curly braces
whenever the body of an if statement or loop includes more than one subordinate statement. The in the Apago PDF Enhancer
last line indicates that the two printed items are different types (“Average score is ” is a string literal and avg is a variable).
Up until now we have used pseudocode, flowcharts, and traces to describe algorithm logic fairly pre- cisely. That precision corresponds closely to the precision found in individual Java-code statements. These algorithmic descriptions have been giving you an informal implementation view of a desired program. The final Java code for that program is a formal implementation view of the program. The people who care most about and see implementation views of programs are the programmers who write those programs.
High-Level Pseudocode
Since pseudocode is so flexible, you can also use it to describe algorithms at a higher, more macroscopic level—with more abstraction. The trick is to ignore the details of subordinate operations and just describe and keep track of inputs to and outputs from those subordinate operations. This strategy presents the “big picture” as seen by the outside world. It looks at the “forest” rather than the “trees.” It helps keep you on the right track—so you don’t solve the wrong problem!
For example, the following Bowling Scores algorithm uses a more high-level pseudocode than what you’ve seen in the past:
Input all scores. Compute average score. Print the average score.
This high-level description presents only the major features, not all the details. It indicates what the program is supposed to do, but not how to do it.