Big Java 4

Chapter 6 – Iteration

Chapter Goals

while Loops

Calculating the Growth of an Investment

Execution of a while Loop

Figure 1 - Execution of a while Loop

ch06/invest1/Investment.java

Your browser does not support the <object> tag.

ch06/invest1/InvestmentRunner.java

Your browser does not support the <object> tag. Program Run:

Animation 6.1: Tracing a Loop

Link to Flash animation

while Loop Flowchart

Figure 2 - Flowchart of a while Loop

while Loop Examples

while Loop Examples

Syntax 6.1 The while Statement

The while Statement

Self Check 6.1

How many times is the following statement in the loop executed?
while (false) statement;

Self Check 6.2

What would happen if RATE was set to 0 in the main method of the InvestmentRunner program?

Common Error: Infinite Loops

Common Error: Off-by-One Errors

Avoiding Off-by-One Error

for Loops

Syntax 6.2 The for Statement

The for Statement

for Loop Flowchart

Figure 3 - Flowchart of a for Loop

Execution of a for Loop

Figure 4 - Execution of a for Loop

Animation 6.2: The for Loop

Link to Flash animation

ch06/invest2/Investment.java

Your browser does not support the <object> tag.

ch06/invest2/InvestmentRunner.java

Your browser does not support the <object> tag. Program Run:

Self Check 6.3

Rewrite the for loop in the waitYears method as a while loop.

Self Check 6.4

How many times does the following for loop execute?
for (i = 0; i <= 10; i++)
   System.out.println(i * i);

for Loop Examples

for Loop Examples

Common Errors: Semicolons

Common Loop Algorithm: Computing a Total

Common Loop Algorithm: Counting Matches

Common Loop Algorithm: Finding the First Match

Common Loop Algorithm: Prompting Until a Match is Found

Common Loop Algorithm: Comparing Adjacent Values

Common Loop Algorithm: Processing Input with Sentinel Values

Loop and a Half

ch06/dataset/DataAnalyzer.java

Your browser does not support the <object> tag.

ch06/dataset/DataSet.java

Your browser does not support the <object> tag. Program Run:

Self Check 6.5

How do you compute the total of all positive inputs?

Self Check 6.6

What happens with the algorithm in Section 6.3.5, Comparing Adjacent Values, when no input is provided at all? How can you overcome that problem?

Self Check 6.7

Why does the DataAnalyzer class call in.next and not in.nextDouble?

Self Check 6.8

Would the DataSet class still compute the correct maximum if you simplified the update of the maximum field in the add method to the following statement?
if (maximum < x) maximum = x;

Nested Loops

ch06/triangle1/Triangle.java

Your browser does not support the <object> tag.

File TriangleRunner.java

Your browser does not support the <object> tag. Program Run:

Nested Loop Examples

Nested Loop Examples

Nested Loop Examples

Nested Loop Examples, continued

Self Check 6.9

How would you modify the nested loops so that you print a square instead of a triangle?

Self Check 6.10

What is the value of n after the following nested loops?
int n = 0;
for (int i = 1; i <= 5; i++)
   for (int j = 0; j < i; j++)
      n = n + j;

Random Numbers and Simulations

ch06/random1/Die.java

Your browser does not support the <object> tag.

ch06/random1/DieSimulator.java

Your browser does not support the <object> tag. Program Run:

Buffon Needle Experiment

Figure 3 - The Buffon Needle Experiment

Needle Position

Buffon Needle Experiment

Figure 4 - When Does the Needle Fall on a Line?

Needle Position

ch06/random2/Needle.java

Your browser does not support the <object> tag.

ch06/random2/NeedleSimulator.java

Your browser does not support the <object> tag. Program Run:

Self Check 6.11

How do you use a random number generator to simulate the toss of a coin?

Self Check 6.12

Why is the NeedleSimulator program not an efficient method for computing π?

Using a Debugger

The Debugger Stopping at a Breakpoint

Figure 5 - Stopping at a Breakpoint

Inspecting Variables

Figure 6 - Inspecting Variables

Debugging

Single-step Example

Self Check 6.13

In the debugger, you are reaching a call to System.out.println. Should you step into the method or step over it?

Self Check 6.14

In the debugger, you are reaching the beginning of a long method with a couple of loops inside. You want to find out the return value that is computed at the end of the method. Should you set a breakpoint, or should you step through the method?

The First Bug

The First Bug