Page 489 - Introduction to Programming with Java: A Problem Solving Approach
P. 489
Note how the empty statement is appropriate here because all the work is done in the for loop header, where i counts up to one billion. All that counting takes time. Depending on your computer’s speed, it might take anywhere from a fraction of a second to five seconds.
So why would you want to add a delay to your program? Suppose you’re writing a game program that needs to have a monster appear for only a certain time interval. To implement that functionality, print the monster, execute the delay loop, and then erase the monster.
You might want to use the above code fragment as part of a first-cut attempt at implementing the delay,
but don’t use it for your final implementation. Why? Because it introduces delay that is dependent on the
speed of the computer that runs the program. With varied delay, slow computers would have monsters that
linger too long and fast computers would have monsters that disappear too quickly. In a final implementa-
tion, you should use the Thread class’s sleep method to implement the delay. The sleep method allows
you to precisely specify the amount of delay in milliseconds. To use the sleep method, you need to under-
3
to put the empty statement on a line by itself? If you put the empty statement on a line by itself and indent it, readers will see it. On the other hand, if you put the empty statement at the end of the previous statement’s line, readers probably won’t see it. Seeing the code is an important part of making the code understandable. And making code understandable makes it easier to maintain.
Avoid Accidental Misuse of the Empty Statement
It’s fairly common for programmers to accidentally create unintended empty statements. Because you enter a semicolon at the end of most lines of Java code, it’s easy to get into the habit of hitting the semicolon key at
System.out.print("Do you want to play a game (y/n)? ");
while (stdIn.next().equals("y"));
stand exception handling, and we discuss exception handling in Chapter 14.
In the above code fragment, note the coding-convention callout. Can you think of why it’s a good idea
Apago PDF Enhancer
the end of every line of code you write. If you do that at the end of a loop header, it generates an empty state- ment. Your code might compile and run without a reported error, but it would produce mysterious results. Here is an example:
11.10 Empty Statement 455
This semicolon creates an empty statement.
{
}
<The code to play the game goes here.>
System.out.print("Play another game (y/n)? ");
Does the semicolon at the end of the while loop header generate a compilation error? No—the semi- colon acts as the lone statement (an empty statement) that’s inside the while loop. The subsequent braces form a compound statement. The compound statement is not part of the while loop; it executes after the while loop has finished.
So what does the code do? First, suppose the user enters n. In the while loop header, the JVM com- pares the entered n value to “y.” The loop condition is false, so the JVM skips the while loop’s body, the empty statement. The JVM then executes the compound statement and attempts to play a game. That’s a logic error: The JVM attempts to play a game even though the user entered n.
Now suppose the user enters y. In the while loop header, the JVM compares the entered y value to “y”. The loop condition is true, so the JVM executes the while loop’s body, the empty statement. The
3 This adds a delay of 1000 milliseconds (which equals 1 second):
try {Thread.sleep(1000);}
catch (InterruptedException e) { }