Java Reference
In-Depth Information
{
double newBalance = balance - amount;
balance = newBalance;
}
A statement such as
balance = balance - amount;
is called a simple statement. A conditional statement such as
if (x >= 0) y = x;
is called a compound statement. In
Chapter 6
, you will encounter loop statements;
they too are compound statements.
The body of an
if
statement or the
else
alternative must be a statementÈŒthat is, a
simple statement, a compound statement (such as another
if
statement), or a block
statement.
S
YNTAX
5.1 The
if
Statement
if
(condition)
statement
if
(condition)
statement
1
else
statement
2
Example:
if (amount <= balance)
balance = balance - amount;
if (amount <= balance)
balance = balance - amount;
else
balance = balance - OVERDRAFT_PENALTY;
Purpose:
To execute a statement when a condition is true or false
184