Java Reference
In-Depth Information
Another red squiggly line! Notice the error message says that a variable must be on the left-hand side of an
assignment. Remember, the single equal sign represents an assignment not a comparison.
Add a second equal sign to make an equal comparison, save the source and run the application.
The evaluation is performed and false is displayed because 1 is not equal to 2.
Tutorial: Ifs
The if statement syntax is: the keyword if , followed by the condition in parentheses, and the statement to be executed
when the condition is true. It is customary to indent the executable statement(s) on the line following the condition.
The indentation signifies that the statement is dependent on the condition.
1.
In CondTestApp, comment out the println statement, add the following statements, save
the source, and run the application.
double doubleSalary = 6.50 * 40;
if (doubleSalary > 550)
System.out.println("The tax rate is 25% or higher");
Are you surprised by the result? Does Figure 6-2 help?
Figure 6-2.
Because doubleSalary is less than 550, nothing is displayed in the console and it seems that the application does
nothing. In actuality, the calculation of doubleSalary and the comparison were performed. Let's prove it.
2.
Add the following statement after the println statement, save the source, and run
the application.
System.out.println(doubleSalary);
 
Search WWH ::




Custom Search