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

                110 Chapter 4 Control Statements
 if (<if-condition>) {
  <if-condition> false
<else-if-condition> false
<else-statement(s)>
<following-statement>
true
<statement(s)>
true
<else-if-statement(s)>
         <if-statement(s)>
else if (<else-if-condition>)
}
{
}
<else-if-statement(s)> ⎫
  .
.⎬ ⎭
optional additional else if’s
  .
else
{⎫ <else-statement(s)> ⎬   optional
}⎭
  Figure 4.3 Syntax and semantics for the “if, else if” form of the if statement
Take several minutes and examine Figures 4.1, 4.2, and 4.3. The figures show the syntax and semantics for the three forms of the Java if statement. The semantics of a statement is a description of how the state-
Apago PDF Enhancer
ment works. For example, Figure 4.1’s flowchart illustrates the semantics of the “if” form of the if state- ment by showing the flow of control for different values of the if statement’s condition.
Most of what you see in the if statement figures should look familiar since it parallels what you learned in Chapter 2. But the “if, else if” form of the if statement deserves some extra attention. You may include as many “else if” blocks as you like—more “else if” blocks for more choices. Note that the “else” block is optional. If all the conditions are false and there’s no “else” block, none of the statement blocks is executed. Here’s a code fragment that uses the “if, else if” form of the if statement to troubleshoot iPod1 problems:
if (iPodProblem.equals("no response"))
{
System.out.println("Unlock iPod's Hold switch.");
else if (iPodProblem.equals("songs don't play"))
}
{
}
else
{
}
System.out.println("Use iPod Updater to update your software.");
System.out.println("Visit http://www.apple.com/support.");
 1 The iPod is a portable media player designed and marketed by Apple Computer.


























































   142   143   144   145   146