Java Reference
In-Depth Information
int lo = 1,
hi = 1;
Notice that the presence of line breaks makes no difference to the
meaning
of
the
statementline
breaks,
spaces,
tabs,
and
other
whitespace are purely for the programmer's convenience.
The while statement in the example demonstrates one way of looping.
The expression inside the while is evaluatedif the expression is true, the
loop's body is executed and the expression is tested again. The while is
repeated until the expression becomes false. If it never becomes false,
the loop will run forever unless something intervenes to break out of the
loop, such as a break statement or an exception.
The body of the while consists of a single statement. That could be a
simple statement (such as a method invocation), another control-flow
statement, or a block zero or more individual statements enclosed in
curly braces.
The expression that while tests is a boolean expression that has the
value true or false . Boolean expressions can be formed with the com-
parison operators ( < , <= , > , >= ) to compare the relative magnitudes of
two values or with the == operator or != operator to test for equality or
inequality, respectively. The boolean expression hi< 50 in the example
tests whether the current high value of the sequence is less than 50. If
the high value is less than 50, its value is printed and the next value is
calculated. If the high value equals or exceeds 50, control passes to the
first line of code following the body of the while loop. That is the end of
the main method in this example, so the program is finished.
To calculate the next value in the sequence we perform some simple
arithmetic and again use the = operator to assign the value of the arith-
metic expression on the right to the variable on the left. As you would
expect, the + operator calculates the sum of its operands, and the - op-
erator calculates the difference. The language defines a number of arith-
metic operators for the primitive integer and floating-point types includ-
 
Search WWH ::




Custom Search