Methods: Communicating with Objects
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 While Structure
A repetition structure is a control structure that repeats a statement or
sequence of statements in a controlled way. Repetition structures are also
referred to as loop structures. Many types of programming tasks require
a repetition structure. Consider some examples.
- You want to add up the squares of the numbers from 1 to 100.
- You want to compute compound interest on an amount of money in a savings account with a fixed interest rate if it is kept there for 30 years.
- A computer security employee wants to try every possible password in order to break into an account of a suspected spy.
- You want to have players input moves for a turn in a game until the game is over. Our OneRowNim is such an example.
Note that in this example, the variable num gets assigned an initial value
of 1 before the while statement. Note also that the boolean expression
num < max in parentheses after while states the condition for which we
wish to continue summing squares. Finally note that the last statement
in the block following the boolean expression adds 1 to num–that is, this
variable is updated at the end of the block.
The while statement is a loop statement in which the loop entry condition occurs before the loop body. It has the following general form:
When the while statement is executed, the loop entry condition is evaluated and if this evaluates to false, execution continues at the statement
immediately after the loop body. If the loop entry condition evaluates to
true, the loop body is executed and then the entry condition is evaluated again. The loop body continues to be executed until the loop entry
condition evaluates to false.
To have a while statement accomplish a task, the variable or variables
in the loop entry condition must be initialized correctly before the while
statement and these variables must be correctly updated at the end of the
loop body. We can refer to the initializer statement followed by a while
statement as a while structure. We can restate the above guidelines as a
In pseudocode, the while structure would take the following form:
As its form suggests, the while structure is designed so that on some conditions the loop body will never be executed. Because it tests for the loop
entry condition before the loop body, it is possible that the loop body is
never executed. We might say that it is designed to perform 0 or more
For example, if the method call sumSquares(-3) is executed, the loop
body will be skipped, because the loop entry condition num <= max is
false to begin with. No iterations will be performed, and the algorithm
will simply return the value 0.
Note also that in the while statement the bound test is preceded by
initializer statements, and the loop body contains updater statements. The
semantics of the while structure are shown in Figure 3.15.
Figure 3.15: Flowchart of the
while statement and while structure.
EXERCISE 3.14 Modify the definition of the sumSquares() method
to define a method named sumCubes() that sums the cubes of integers
from a minimum value up to a maximum value and returns that sum.
sumCubes() should have two parameters that will store the minimum
and maximum values. Thus the method call sumCubes(2,3) should
return 35 since 2 ⇤ 2 ⇤ 2+3 ⇤ 3 ⇤ 3 = 8+27 =