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 happen.
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, or int.
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 );