Java Reference
In-Depth Information
Figure 6-4.
5.
Change the source so that an
else
clause follows the closing brace as follows:
if
(doubleSalary > 550) {
System.out.println("The tax rate is 25% or higher");
System.out.println(doubleSalary);
}
else
{
System.out.println("The tax rate is 15% or lower");
System.out.println("Somebody needs a raise!");
}
Again it is customary (but not required) to slightly indent the
else
to denote that the else is associated with the
if
.
This makes the code much easier to understand, especially when there are multiple ifs and elses. Multiple statements
can also be tied to an
else
. Just as with the
if
, simply enclose the statements in braces. The logic flow can be seen
in Figure
6-5
.