We communicate with objects using methods. Methods are executable code within each object, for which an interface has been established. Sometimes the interface is only for the object itself. Other times it is an interface accessible by other objects. This chapter discusses that topic in detail.
3.6 Flow of Control: Control Structures
The Simple If Statement
A selection control structure, allows a program to select between two or more alternative paths of execution. The if statement is the most basic selection control structure in Java. Most programming languages have their own Simple if statement equivalent.
The statement contained in the if statement can be any valid Java statement, including a compound statement. (Recall from Chapter 1 that a compound statement is a set of statements contained within curly braces.) The boolean expression is an
expression that is either true
or false. We have seen examples of boolean expressions that involve
int variables, int values, and the inequality or equality operators. A method call to a method with a boolean result type is another example of a boolean expression. Given this description of if statement syntax, the
following are examples of valid if statements:
For readability, we usually write an if statement with its contained statement indented on the next line:
The following are all examples of syntax errors involving the if statement:
Semantically, the
if statement has the following interpretation: First, the boolean condition is evaluated. If it is true, then the contained statement is executed; if it is false, then the contained statement is not executed. This is shown in Figure 3.11. The
flowchart clearly shows that program flow will take one or the other of the alternative paths coming out of the diamond shaped boolean condition box. The branch through the rectangular statement box will be taken when the boolean condition is
true; otherwise the statement will be skipped.
As another example, consider the definition of a getPlayerString()
method for the OneRowNim class:
The flowchart in Figure 3.12 shows the program flow of the entire
getPlayerString() method. It is important to note that when a return statement is executed in a method, control is returned immediately to the calling method. Thus, if player == 1 is true,
the string “Player One” is returned to the calling method and the
getPlayerString() method exits at this point. If it is false, then
player == 2 should be true (if we have a consistent state) and the string
“Player Two” should be returned and the method exited. Thus, if we have
a consistent state —that is, if player has value 1 or 2—then the third
return statement should never be reached.
Figure 3.12: Flowchart of the
getPlayerString() method
The following example shows the more common case where the stateCompound statement ment contained in an if statement can be a compound statement:
If player == 1 is true, then all four statements in the contained comLocal scope pound statement will be executed. Note here that we are declaring the
local variable, s, in this block. Its scope would extend only to the end of
the block. Note also that when we use a compound statement, the compound statement itself is not followed by a semicolon because it is already
enclosed in braces.
A common programming error is to forget the braces around the compound statement. Merely indenting the statements following the if clause
doesn’t alter the logic of the if statement. For example, the following if
statement still has only one statement in its if clause:
This segment will always print “Two” because the second println() is
not part of the if statement. To include it in the if statement, you must
enclose both println() statements within braces: