Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^236) | File Objects and Looping Statements


The Loop Exit


When the termination condition occurs and the flow of control passes to the statement fol-
lowing the loop, the variables used in the loop still contain values. Also, if an input file has
been used, the reading marker has been left at some position in the file. Or maybe an out-
put file has new contents. If these variables or files are used later in the application, the loop
must leave them in an appropriate state. For this reason, the final step in designing a loop
is answering this question:
 What is the state of the code on exiting the loop?
Now we have to consider the consequences of our design and double-check its validity.
For example, suppose we’ve used an event counter and later processing depends on the
number of events. It’s important to confirm (with an algorithm walk-through) that the value
left in the counter is the exact number of events—that it is not off by 1.
Look at this code segment:
lineCount = 1; // This code is incorrect
while((inLine = inFile.readLine()) != null)
lineCount++;
System.out.println("There are "+ lineCount + " lines in the file.");
This loop reads lines from an input file and counts the number of lines in the file. However,
when the loop terminates,lineCountequals the actual number of lines plus 1 because the
loop initializes the event counter to 1 before any events take place. By determining the state
oflineCountat loop exit, we’ve detected a flaw in the initialization.lineCountshould be initialized
to zero. Note that this code segment also demonstrates the use of an assignment expression
within a loop test. Because the loop contains just a single statement (the increment statement),
there is no need for the usual braces to enclose a block of statements.
Designing correct loops depends as much on experience as it does on the application of de-
sign methodology. At this point, you may want to read through the Problem-Solving Case Study
at the end of the chapter to see how the loop design process is applied to a real problem.

Nested Loops


In Chapter 4, we described nested ifstatements. It’s also possible to nest whilestatements.
Both whileand ifstatements contain statements and are themselves statements. So the
body of a whilestatement or the branch of an ifstatement can contain other whileand if
statements. By nesting, we can create complex control structures.
Suppose we want to count the commas in each line, repeating this operation for all the
lines in a file. We put an event-controlled loop within the main loop. The inner loop uses the
charAtmethod of the Stringclass, which returns the character at a given position in the
string. The loop scans the line, position by position, searching for commas.
lineCount = 0; // Initialize iteration count
inLine = inFile.readLine(); // Priming read
while(inLine != null) // Outer loop test for EOF
{
lineCount++; // Update outer iteration count
Free download pdf