Statement Forms
4.1 Simple statements
In the programs in Chapters 2 and 3, you saw simple statements used to accomplish a variety of tasks. For example, the very first program in the text was
add(new GLabel("hello, world"), 100, 75);
which was a call to the add method in GraphicsProgram. Similarly, you have been introduced to assignment statements, such as the shorthand form
balance += deposit;
and statements that display information, such as
println("The total is " + total + ".");
Informally, it makes sense to think of these statement types as separate tools and to use them idiomatically. If you need to read an integer, all you need to do is remember that there is an idiom for that purpose, which you can then write down. If you need to display a value, you know that you should use the println method. Viewed formally, however, these simple statements all have a unified structure that makes it easy for the Java compiler to recognize a legal statement in a program. In Java, all simple statements— regardless of their function—fit the following rule:
Simple Statement Rule
A simple statement consists of an expression followed by a semicolon.
Thus, the template for a simple statement is simply this:
expression;
Adding the semicolon after the expression turns the expression into a legal statement form.
Even though any expression followed by a semicolon is a legal statement in Java, it is not true that every such combination represents a useful statement. To be useful, a statement must have some discernible effect. The statement
n1 + n2;
consists of the expression n1+n2 followed by a semicolon and is therefore a legal statement. It is, however, an entirely useless one because nothing is ever done with the answer; the statement adds the variables n1 and n2 together and then throws the result away. Simple statements in Java are typically assignments (including the shorthand assignments and increment/decrement operators) or calls to methods, such as println, that perform some useful operation.
It is easy to see that program lines such as
println("The total is " + total + ".");
are legal statements according to the Simple Statement Rule. In the definition of expression given in Chapter 2, method calls are legal expressions, so that the method call part of the above line—everything except the semicolon—is a legal expression. Putting the semicolon at the end of the line turns that expression into a simple statement.
But what about assignments? If a line like total = 0;
is to fit the Simple Statement Rule, it must be the case that total = 0
is itself an expression.
In Java, the equal sign used for assignment is simply a binary operator, just like + or /. The = operator takes two operands, one on the left and one on the right. For our present purposes, the left operand must be a variable name. When the assignment operator is executed, the expression on the right-hand side is evaluated, and the resulting value is then stored in the variable that appears on the left-hand side. Because the equal sign used for assignment is an operator,
total = 0
is indeed an expression, and the line total = 0;
is therefore a simple statement. Blocks
Simple statements allow programmers to specify actions. Except for the HelloProgram example in Chapter 2, however, every program you have seen so far requires more than one simple statement to do the job. For most programs, the solution strategy requires a coordinated action consisting of several sequential steps. The Add2Integers program, for example, had to first get one number, then get a second, then add the two together,
and finally display the result. Translating this sequence of actions into actual program steps required the use of several individual statements that all became part of the main program body.
To specify that a sequence of statements is part of a coherent unit, you can assemble those statements into a block, which is a collection of statements and declarations enclosed in curly braces, as follows:
{ statement1 statement2 statement3 . . . statementn }
Any of these statements can be replaced by a declaration, and it is probably easiest to think of declarations as simply another form of statement.
You have already seen blocks in several of the programming examples from the previous chapters. The body of each method is a block, as are the bodies of the control statements introduced later in this chapter.
The statements in the interior of a block are usually indented relative to the enclosing context. The compiler ignores the indentation, but the visual effect is helpful to the human reader, because it makes the structure of the program jump out at you from the format of the page. Empirical research has shown that using either three or four spaces at each new level makes the program structure easiest to see; the programs in this text use four spaces for each new level. Indentation is critical to good programming, so you should strive to develop a consistent indentation style in your programs.
When the Java compiler encounters a block, it treats the entire block as a single statement. Thus, whenever the notation statement appears in a syntax template or an idiomatic pattern, you can substitute for it either a single statement or a block. To emphasize that they are statements as far as the compiler is concerned, blocks are sometimes referred to as compound statements.