Java Reference
In-Depth Information
In this topic, the Java programming concepts and techniques are presented in a logical
order. When these concepts and techniques are learned one at a time in a logical order,
they are simple enough to be understood completely. Understanding a concept or
technique completely before using it will save you an enormous amount of debugging
time.
Program Style and Form (Revisited): Indentation
In the section ''Program Style and Form'' of Chapter 2, we specified some guidelines to
write programs. Now that we have started discussing control structures, in this section,
we give some general guidelines to properly indent your program.
As you write programs, typos and errors are unavoidable. If your program is properly
indented, you can spot and fix errors quickly as shown by several examples in this
chapter. Typically, the IDE that you use will automatically indent your program. If for
some reason your IDE does not indent your program, you can indent your program
yourself.
Proper indentation can show the natural grouping of statements. You should insert a
blank line between statements that are naturally separate. In this topic, the statements
inside braces, the statements of selection structures, an if statement within an
if statement are all indented four spaces to the right. Throughout the topic, we
use four spaces of indentation for statements; we especially use indentation to show
the level of a control structure within another control structure. You can also use
four spaces for indentation.
There are two commonly used styles for placing braces. In this topic, we place braces on a
line by themselves. Also, matching left and right braces are in the same column, that is,
they are the same number of spaces away from the left side of the program. This style of
placing braces easily shows the grouping of the statements as well as matching left and
right braces. You can also follow this style to place and indent braces.
In the second style of placing braces, the left brace need not be on a line by itself.
Typically, for control structures, the left brace is placed after the last right parenthesis of
the (logical) expression and the right brace is on a line by itself. This style might save some
space. However, sometimes this style might not immediately show the grouping or the
block of the statements.
No matter what style of indentation you use, you should be consistent within your
programs and the indentation should show the structure of the program.
switch Structures
Recall that there are three selection, or branch, structures in Java. The two-selection
structure, which is implemented with if and if ... else statements, usually requires the
evaluation of a (logical) expression. The third selection structure, which does not require
 
Search WWH ::




Custom Search