Java Reference
In-Depth Information
one and one half times the regular rate for all hours after the first 40 hours worked.
When the employee works 40 or more hours, the pay is then equal to
rate*40 + 1.5*rate*(hours - 40)
However, if the employee works less than 40 hours, the correct pay formula is simply
rate*hours
The following
statement computes the correct pay for an employee whether
the employee works less than 40 hours or works 40 or more hours:
if-else
if (hours > 40)
grossPay = rate*40 + 1.5*rate*(hours - 40);
else
grossPay = rate*hours;
The syntax for an
statement is given in the box entitled “
Statement.”
if-else
if-else
If the Boolean expression in parentheses (after the
) evaluates to
, then the
if
true
statement before the
is executed. If the Boolean expression evaluates to
,
else
false
then the statement after the
is executed.
Remember that when you use a Boolean expression in an
else
statement, the
if-else
parentheses
Boolean expression must be enclosed in
parentheses
.
statement has smaller statements embedded in it. Most of
the statement forms in Java allow you to make larger statements out of smaller ones by
combining the smaller statements in certain ways.
Notice that an
if-else
Omitting the
else
Sometimes you want one of the two alternatives in an
statement to do noth-
if-else
ing at all. In Java, this can be accomplished by omitting the
part. These sorts of
else
if statement
statements are referred to as
statements
to distinguish them from
state-
if
if-else
ments. For example, the first of the following two statements is an
statement:
if
if (sales > minimum)
salary = salary + bonus;
System.out.println("salary = $" + salary);
If the value of
is greater than the value of
, the assignment statement
sales
minimum
is executed and then the following
statement is executed. On
System.out.println
the other hand, if the value of
is less than or equal to
, then the
sales
minimum
embedded assignment statement is not executed, so the
statement causes no
change (that is, no bonus is added to the base salary), and the program proceeds
directly to the
if
statement.
System.out.println
Search WWH ::




Custom Search