Chapter 4. Statements and Comments

  • Organizing Statements

  • Expression Statements

  • Selection Statements

  • Looping Statements

  • Transfer of Control Statements

  • Comments

  • Javadoc

  • Exercises

  • Some Light Relief—MiniScribe: The Hard Luck Hard Disk

Huh? What is it, Lassie? What is it, girl?

You say someone has left the braces off their if statement, and that it's likely to be a maintenance problem? Good girl, Lassie!

Statements are the way we get things done in a program. Here are the most common statements in Java.

Organizing statements

{ /*statements*/ }

and

emptyStatement ;

Expression statement

someExpression ;

Selection statements

if
switch

Looping statements

for
while

Transfer of control

return

There are two statements, “throw” and “catch” relating to a form of error handling known as “exceptions”. There is also a statement called the “thread statement” which uses the “synchronized” keyword. When there are several threads contending for a shared resource, the synchronized keyword serializes access to the resource, the same way a turnstile allows a crowd of people to enter a stadium one at a time. The thread statement and the exception statements are covered in their own chapters later. The statements described in this chapter, plus Chapter 10, “Exceptions,” Chapter 13, “Doing Several Things at Once: Threads,” and Chapter 14, “Advanced Thread Topics,” cover all statements in Java.

Statements cannot float loose in programs—they are always inside a block. The Java Language Specification says that a block is itself a statement, which makes sense because it is the way you group a number of related statements. A “block” is a programming language term for a pair of curly braces plus everything inside it. A block contains local variable declarations, class declarations, and statements, collectively known as the block statements. Here is an example block.

{
     int i = 0;                   // block statement
     class x { /* some code */ }  // block statement
     i = 27;                      // block statement
}

A method body is a block.

public static void main(String[]args) {
    /* an empty method body block */
}

Any statement may be prefixed by a label, as shown here:

processMonths:
    for(int m = 1; m <= 12; m++) { ...

Java doesn't have a goto statement that could be used to branch to a label. If a label appears, it is either just empty documentation (rare—use a comment instead), or it is used to break out of certain enclosing statements in a clean way.

Even though there is no goto statement, there is a reserved word “goto” in Java. The designers grabbed the keyword to ensure that no one uses it as a variable name. That way, if it later turns out to be convenient to support a goto (perhaps for automatically generated code, or to fix the appalling “switch” statement), it can be introduced without breaking any existing code.

Most of Java's statements are very similar to their counterparts in other languages and will be readily recognizable to any programmer. Accordingly, we can limit our discussion to showing the general form of each statement and noting any special rules or “gotchas” that apply.

Organizing Statements

Wherever you can legally put one statement, you can put a block of several statements. We've seen blocks many times. Use them to group statements in a loop or an “if” clause, or to declare a variable that will be used only within the block. This is called a local variable because it is local to the block and the name cannot be seen outside the block. A block looks like this:

for (;;) {
    int timeMsecs = 500;      // local variable
    Thread.sleep(timeMsecs);
}

This makes the entire block be the subject of the “for” loop. In this example, the for statement causes the block to be repeated in an infinite loop. It can be stopped by an exception error condition or by something external to the loop, like closing the window containing the program, or stopping the Thread it is running in.

Empty statement

Java has the empty statement, which is simply a semicolon by itself. The empty statement does nothing, and is used to make things clearer. In the example below, we are saying “if the condition is true, do nothing; otherwise, invoke the method.”

boolean noRecordsLeft = /*some calculation*/
if (noRecordsLeft)

      ; // empty statement

else
      alertTheMedia();

The code is written this way to make it clearer. If you rewrite it so the “else” part becomes the “then” part to avoid an empty statement, the condition becomes a double negative (“not no records left”).

if (noRecordsLeft==false) {

          alertTheMedia();
}

Double negatives don't unconfuse code, so if the empty statement helps you never fail to avoid them, then don't not go for it. Wherever you can legally put a statement, you can put an empty statement. It's usual to comment the empty statement and put it on a line by itself.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.124.21