|
|
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
A Java program is organized into a sequence of statements, source code constructs that declare variables, assign expression results to variables, repeatedly execute other statements in loops, control loops, make decisions, and so on. Java bases its syntax for expressing statements in source code on C/C++ syntax. That similarity in syntaxes simplifies the transition from C/C++ to Java. As we explore statements, I will point out the few syntactic differences.
As with C/C++, Java supports two categories of statements: simple and compound. A compound statement is also known as a block.
You express the syntax of a simple statement as follows:
( vardecl | assign | loop | loop control | decision | other )
This syntax shows that a simple statement is either a variable declaration statement, an assignment statement, a loop statement, a loop control statement, a decision statement, or some other kind of statement. I will explain each of these simple statement categories in subsequent sections.
You express a compound statement's syntax as follows:
'{' [ ( simple_statement | compound_statement ) ... ] '}'
That syntax shows that a compound statement consists of zero or more simple and compound statements surrounded by brace characters.
Finally, you express a statement's syntax as follows:
( simple_statement | compound_statement )
The syntax above shows that a statement is either a simple statement or a compound statement.
A variable declaration statement declares and initializes one or more variables of a certain type. For more information on declaring/initializing variables (including the variable declaration statement's syntax), check out the variables section in Part 1.
Example:
int i, k; double j = 1.0; String s = "abc";
The above example illustrates three variable declaration statements. The first statement declares a pair of int variables -- i and k. Each variable is either initialized to a default value or left in an uninitialized state. (I will discuss more on initialization
and default values in upcoming articles.) The second statement declares double variable j and initializes that variable to double literal 1.0. Finally, the third statement declares a String variable -- s -- and initializes it to "abc". (I will discuss more on String in a future article.)
An assignment statement assigns an expression's result to a variable. The following syntax expresses an assignment statement in source code:
variable_name '=' expression ';'
expression is evaluated, and the resulting value is assigned to variable_name -- a previously declared variable.
Example:
int i, j; i = (int) 3.5; // Data types must match! That is the reason for the cast. j = ++i;
The example above first uses a variable declaration statement to declare a pair of int variables -- i and j. Second, an assignment statement that evaluates expression 3.5 casts the data type from double-precision floating-point to integer (to prevent the compiler from generating an error), and
assigns the result of the cast -- 3 -- to i. Finally, the third assignment statement initializes j. That last statement first evaluates expression ++i -- i increments from 3 to 4 -- and then assigns the expression's result to j.
Example:
int x, y = 2; x = y += 3;
The previous example demonstrates that an assignment statement is also an expression. First, a pair of integer variables --
x and y -- is declared. y is initialized to 2. Then, the assignment statement x = y += 3; executes. That statement first executes the assignment statement/expression y += 3, which adds 3 to y's value of 2. Then, the resulting value -- 5 -- is assigned to x.
A loop statement repeatedly executes another statement. The following syntax expresses a loop statement in source code:
( while | do-while | for )
The syntax above shows that a loop statement is either a while loop statement, a do-while loop statement, or a for loop statement. Each loop statement has an analogue in C/C++.
The while loop statement evaluates a Boolean expression and, if this expression is true, executes another statement. After that other statement executes, the while loop statement reevaluates the Boolean expression. If it is still true, the other statement reexecutes. That cycle repeats until the Boolean expression evaluates to false. A while loop statement is known as an entry condition loop because it evaluates the Boolean expression at its top, or entry point. Use the following syntax to express a while loop statement in source code:
'while' '(' Boolean_expression ')'
statement
As long as Boolean_expression evaluates to true, statement executes. In essence, a while loop statement represents two statements that are specified together.
Example:
int i = 0;
while (i < 10)
System.out.println ("i = " + i++);
Variable i in the above code is used as a loop control variable -- a variable that keeps track of the current loop iteration. That variable initializes to 0. As long as i is less than 10, System.out.println ("i = " + i++); executes. (The expression "i = " + i++ is evaluated as follows: the value of i is extracted, i is incremented, the extracted value is converted into a string, and that string is concatenated to "i = ".)
The do-while loop statement executes a statement and then evaluates a Boolean expression. If that expression is true, the statement reexecutes. The do-while loop statement then reevaluates the Boolean expression. If it is still true, the statement reexecutes. This cycle repeats itself until the Boolean expression evaluates to false. A do-while loop statement is known as an exit condition loop because it evaluates the Boolean expression at its bottom, or exit point. Use the following syntax to express a do-while loop statement in source code:
'do'
statement
'while' '(' Boolean_expression ')'
statement executes at least once in the code above. Then, as long as Boolean_expression evaluates to true, statement reexecutes. Like the while loop, a do-while loop statement represents two statements that are specified together.
Example:
int ch;
do
{
System.out.println ("Enter y for yes or n for no:");
ch = System.in.read ();
}
while (ch != 'y' && ch != 'n');
System.out.println ((char) ch);
The above example demonstrates the difference between a while loop and a do-while loop: The user is prompted to enter y or n. Next, calling System.in.read inputs the user's choice, which is assigned to variable ch. If the user has entered y or n, the Boolean expression evaluates to false and the loop exits. Otherwise, the loop reexecutes the compound statement -- the
prompt and input. A do-while loop statement differs from the while statement in that it executes at least once; a while loop
statement might never execute.
The for loop statement repeatedly executes another statement under the control of one or more loop control variables. Use the following syntax to express a for loop statement in source code:
'for' '(' [ loop-init ] ';' [ loop-test ] ';' [ loop-adjust ] ')'
statement
The loop-init clause is either a nonarray variable declaration statement or a comma-delimited list of assignment statements. That clause
initializes one or more loop control variables and executes only once -- when the for loop is first entered.
Following the execution of loop-init in the code above, the for loop's loop-test clause executes. loop-test consists of a single Boolean expression that determines whether or not the loop should proceed. If that expression evaluates
to true, the for loop proceeds by executing statement. Otherwise, the for loop exits and execution continues with the statement following for (...) statement. A for loop statement is known as an entry condition loop, because it evaluates the Boolean expression at its top, or entry
point.
Once statement has completed execution, loop-adjust executes. That clause modifies the loop control variable(s). The reexecution of loop-test follows the execution of loop-adjust. If loop-test still evaluates to true, statement reexecutes, followed once more by loop-adjust. That cycle continues until loop-test returns false. Basically, a for loop statement represents two statements that are specified together.
Example:
for (int i = 0; i < 10; i++)
System.out.println ("i = " + i);
In the example immediately above, the for loop initializes integer loop control variable i to 0, evaluates the Boolean expression i < 10, and executes System.out.println ("i = " + i);, as long as i is less than 10. Immediately after System.out.println() executes, i++ executes to advance i. After that advance, i < 10 reexecutes to ensure that i is still less than 10. As soon as i is equal to 10, the loop terminates.
A loop control variable doesn't have to be an integer. It can be of another data type such as Boolean, character, and double-precision floating-point, as shown below: