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




Custom Search