Java Reference
In-Depth Information
C ODE B LOCK
A block of code is a set of Java statements grouped by curly braces {}. Blocks can
also have other blocks nested inside them.
{this is a statement in a block of code;
this is another statement in the same block of code;
{this is a statement in a block nested in the first;
this is another statement in a nested block;
}
// This is the end of the nested code block.
}
// This is the end of the first code block.
COBOL organizes statements through the use of verbs such as if , else , and
end...if . The end-of-sentence character (period) also defines the end of any con-
ditional group of statements. In Java, multiple statements that are executed as a re-
sult of some flow control expression (such as if and else ) must be grouped into a
code block using the braces.
However, the addition of another statement to the code block would require
that these two statements be grouped with braces. Therefore, it is good practice to
place braces around single-statement code blocks, especially if they are the result of
some conditional expression.
By convention, the statements in a code block are indented to the same column.
This helps to visually organize the statement groups and improves readability. (Sun
defines a set of code conventions at http://java.sun.com/docs/codeconv/html/Code-
ConvTOC.doc.html.)
In addition, it is good programming practice to build your code blocks (that is,
create the if and else sections, including the braces) before adding the statements
inside the code block. This allows you to concentrate on the two steps indepen-
dently and, therefore, reduces the number of compile errors and runtime bugs cre-
ated by syntax mistakes.
T HE IF S TATEMENT
Structure: if (condition) { block }
else {block} ;
 
Search WWH ::




Custom Search