Page 66 - Introduction to Programming with Java: A Problem Solving Approach
P. 66

                32 Chapter 2 Algorithms and Design
Note the angled brackets “<>” that surround “condition” and “statement(s).” Throughout the book, we use the italics and angled bracket notation for items that require a description. Thus, when you see “<condi- tion>,” it tells you that an actual condition, not the word “condition,” is supposed to follow the word “if.” Likewise, when you see “<statement(s)>,” it tells you that one or more actual statements, not the word “statement(s),” is supposed to go underneath the if statement’s heading.
In the above if statement illustration, note how <statement(s)> is indented. Pseudocode emulates a natural-language outline by using indentation to show encapsulation or subordination. The statements under an if statement’s heading are subordinate to the if statement because they are considered to be part of the larger, encompassing if statement. Since they are subordinate, they should be indented.
Here’s how the simple “if” form of the if statement works:
• If the condition is true, execute all subordinate statements, that is, execute all indented statements im- mediately below the “if.”
• If the condition is false, jump to the line after the last subordinate statement, that is, jump to the first un-indented statement below the “if.”
Let’s put these concepts into practice by showing you an if statement in the context of a complete algorithm. Figure 2.7’s Shape algorithm prompts the user for a shape. If the user enters “circle,” the algorithm prompts for a radius, calculates the area of a circle using that radius, and prints the resulting area. Finally, regardless of whether the user entered “circle” or not, the algorithm prints a friendly end-of-algorithm message.
 print “What is your favorite shape? ”
           Apago PDF Enhancer
set area to 3.1416 * radius * radius
print “The area is” area
print “End of shape algorithm. Seeya!”
Sample session when shape is a circle:
input shape
if shape isacircle
condition
      print “Enter a radius value: ”
⎫ input radius ⎢
⎬ ⎢ ⎭
    What is your favorite shape? circle
Enter a radius value: 2
The area is 12.5664.
End of shape algorithm. Seeya!
Sample session when shape is not a circle:
 What is your favorite shape? trapezoid
End of shape algorithm. Seeya!
These four statements are subordinate to the encompassing if statement.
Figure 2.7 Shape algorithm that calculates a circle’s area if the user’s favorite shape is a circle
You should take note of several items in the Shape algorithm. “shape is a circle” is the if statement’s condition. It controls whether the if statement’s subordinate statements execute. Note how the set area command and subsequent print command are separate statements. That’s perfectly acceptable and quite common, but you should be aware of an alternative implementation where the two commands are merged into one statement:






































































   64   65   66   67   68