CS
99
Summer
2001 7.09
Lecture Notes 4
Statements
A
statement in Java is basically a line of code terminated by a semicolon. Declarations and statements can be grouped
into a block using braces, {}. This is
very important, as a block is a compound statement which can be used to group
zero or more local declarations and statements; it can be used in any context
where a simple statement is permitted.
Statements
in Java can be grouped into various categories:
·
declaration
statements
Examples:
int
x; double y;
TokenReader
in;
·
flow
control statements
Examples:
if
( x > y )
x = 2*y;
while
( z < 10 ) {
System.out.println("2 to the power
of " + z + " = " + Math.pow( 2, z++ ) );
}
·
expression
statements
Only certain types of expressions have meaning as statements
·
Assignments
Examples:
x
= 10;
new_balance
= old_balance*(1 + bank_rate);
·
Increment
and decrement operators
Examples:
x++;
y--;
++z;
·
Method
calls
Example:
int
n = s.length();
·
Object
creation with the new operator
Examples:
TokenReader in = new
TokenReader( System.in );
Triangle t = new Triangle( 3, 4, 5 );
Booleans
The
boolean datatype is used to represent logical values that can be either the
literal true
or the literal false.
Boolean
expressions have boolean datatype and can only evaluate the values true or false.
Boolean
expressions, when used as conditionals in control statements, allow the control
flow in a program to be changed during execution.
Relational
Operators <, <=, >, >=
Equality:
==, !=
Boolean
Logical Operators: !, ^, &, |
Conditional
Operators: &&, ||
Examples
of boolean expressions:
Simple if
statement
The
simple if
statement has the following syntax
if ( <conditional expression> )
<statement>
Note
that the statement can be a block.
if-else
statement
if ( <conditional statement> )
<statement1>
else
<statement2>
The
use of block notation {} can be critical to the meaning of if statements. The Java compiler always associates an else
with the previous if unless told to do otherwise by braces. This is sometimes known as the dangling-else
problem:
if ( x > 5 )
if ( y > 5)
System.out.println(
"x and y are > 5" );
else
System.out.println(
"x is <=5" );
The
person who wrote this clearly wanted the else to be matched with the first
if,
for two reasons: the program is indented to suggest that that is it's purpose,
and the println statement only makes sense if it were that way. However, Java matches an else with the last if if there are no braces to
tell it to do otherwise. So to the
computer, the code actually looks like this:
if ( x > 5 )
if ( y > 5)
System.out.println(
"x and y are > 5" );
else
System.out.println( "x is
<=5" );
Surely
not what the programmer intended. To
fix the segment, the programmer should insert braces around the second if statement:
if ( x > 5 ) {
if ( y > 5)
System.out.println(
"x and y are > 5" );
}
else
System.out.println( "x is
<=5" );
Now
the program segment works properly.
Repetition Structures
Loops
allow a block of statements to be executed repeatedly. A boolean condition is commonly used to
determine when to terminate the loop, called the loop condition. The statements executed in the loop
constitute the loop body. The loop body
can be a single statement or a block.
while Statement
while ( <loop
condition> )
<loop body>
Note
that the loop condition is evaluated before
executing the loop body. The while
statement executes the loop body as long as the loop condition is true. When the loop condition becomes false, the
loop is terminated and execution continues with the statement immediately
following the loop.
do-while Statement
do
<loop body>
while ( <loop
condition> );
The
loop condition in this construct is evaluated after executing the loop body.
Note that the loop body is executed at least once.
To
illustrate the difference between these statements consider the following code
segments:
while ( cat.isAway()
) {
mice.willPlay();
}
do {
mice.willPlay();
} while ( cat.isAway() );
In
the first loop, the mice look to see if the cat is away before they go out to play; in the second loop, the mice play
before they look to see if the cat is away.
The second group of mice get to play at least once, but it might be
their last playtime!