Java Reference
In-Depth Information
Notice that the value of doubleSalary (260.0) is displayed, proving that the calculation is being performed. Why
was the doubleSalary value displayed but not the text “ The tax rate is 25% or higher ”? Because only one statement
is tied to the condition (see Figure 6-3 ). To associate more than one statement to a condition, the statements must be
enclosed in braces. Because we did not have the println statement inside braces, the value is displayed whether the
condition is true or false.
Figure 6-3.
3.
Change the source code so that there is an opening brace after the condition and a closing
brace after the last statement (see below).
if (doubleSalary > 550) {
System.out.println("The tax rate is 25% or higher");
System.out.println(doubleSalary);
}
Notice that it is customary to place the closing brace on the line following the executable statements and to align
the closing brace with the if keyword.
4.
Save the source and run the application.
Nothing appears again! Well, that's right, because the condition is still false. Our logic flow (as seen in Figure 6-4 )
still dictates that nothing is done when the condition is false. Let's code an else clause so that when the condition is
false, something happens.
Search WWH ::




Custom Search