Control Statements and Loops
Expression / Statement
An assignment or increment / decrement expression can be made into
a statement by adding a semi-colon. An expression statement is
executed by evaluating the expression.
Empty Statement
An empty statement does nothing:
;
if Statement
The if statement allows conditional execution of a statement
(with a single if ) or a conditional choice of two statements
(with if-else ), executing one or the other but not both.
if ( Expression )
Statement
If the Expression evaluates to true, then Statement
is executed. If the Expression evaluates to false then
nothing happens.
if ( Expression )
Statement1
else
Statement2
If the Expression evaluates to true, then Statement1
is executed. If the Expression evaluates to false then
Statement2 is executed.
Switch Statement
The switch statement transfers control to one of several
statements depending on the value of an Expression. The type of the
Expression must be char, byte, short, int or String.
switch (k)
{
case 0: System.out.println ("zero"); break;
case 1: System.out.println ("one"); break;
default: System.out.println ("out of range"); break;
}
The body of a switch statement is known as a switch block. Any statement
immediately contained by the switch block may be labeled with one or more
case or default labels. When the switch
statement is executed, first the Expression is evaluated. If one of the
case constants is equal to the value of the expression, then all
the statements after the matching case label in the switch
block are executed. If no case matches but there is a
default label, then all the statements after the matching default
label are executed. If there is a break statement then the
switch statement completes execution.
Conditional Operator ? :
The conditional operator ? : uses the boolean value of one
expression to decide which of the other two expresions should be evaluated.
max = ( a > b ) ? a : b;
The first expression must be of type boolean.
while Statement
The while statement executes an Expression and a
Statement repeatedly until the value of the Expression
is false. The Expression must have type boolean. If the
value of the Expression is false the first time it is
evaluated, then the Statement is not executed.
while ( Expression )
Statement
do Statement
The do statement executes an Expression and a
Statement repeatedly until the value of the Expression
is false. The Expression must have type boolean. If the
value of the Expression is false the first time it is
evaluated, then the Statement is executed at least once.
do
Statement
while ( Expression );
for Statement
The for statement executes some initialization code, then
executes an Expression, a Statement, and some update code repeatedly
until the value of the Expression is false.
for ( Init; Expression; Update )
Statement
The Init code is a list of statement expressions, the expressions are
evaluated in sequence from left to right. If in the Init code, there is
a local variable declaration then the scope of the local variable is in
the for block. If the value of the Expression is false the
first time it is evaluated, then the Statement is not executed.
Labeled Statements
Statements may have label prefixes.
Identifier : Statement
The Java programming language has no goto statement. The identifier
statement labels are used with break and continue
statements.
break
The break transfers control out of an enclosing statement.
break Identifier;
The Identifier is optional. If there is an Identifier, then break
attempts to transfer control to the statement having the same label as the
Identifier.
continue Statement
The continue statement may occur only in a while, do,
or for statement. The continue statement ends the current
iteration and begins a new one.