Java Reference
In-Depth Information
if( x == 0 )
System.out.println( "x is zero" );
else
{
System.out.print( "x is " );
System.out.println( x );
}
The if statement can itself be the target of an if or else clause, as can
other control statements discussed later in this section. In the case of nested
if-else statements, an else matches the innermost dangling if . It may be
necessary to add braces if that is not the intended meaning.
1.5.4 the while statement
Java provides three basic forms of looping: the while statement, for statement,
and do statement. The syntax for the while statement is
The while state-
ment is one of
three basic forms
of looping.
while( expression )
statement
next statement
Note that like the if statement, there is no semicolon in the syntax. If one is
present, it will be taken as the null statement.
While expression is true , statement is executed; then expression is reevalu-
ated. If expression is initially false , then statement will never be executed. Gen-
erally, statement does something that can potentially alter the value of expression ;
otherwise, the loop could be infinite. When the while loop terminates (nor-
mally), control resumes at the next statement.
1.5.5 the for statement
The while statement is sufficient to express all repetition. Even so, Java
provides two other forms of looping: the for statement and the do statement.
The for statement is used primarily for iteration. Its syntax is
The for statement
is a looping con-
struct that is used
primarily for simple
iteration.
for( initialization ; test ; update )
statement
next statement
Here, initialization , test , and update are all expressions, and all three are
optional. If test is not provided, it defaults to true . There is no semicolon
after the closing parenthesis.
The for statement is executed by first performing the initialization . Then,
while test is true , the following two actions occur: statement is performed, and
 
Search WWH ::




Custom Search