Java Reference
In-Depth Information
When trying out the example, you started by entering
32
, so that
degFahren
will be initialized to
32
. In
this case the calculation
degCent = 5/9 * (degFahren
−
32)
will set
degCent
to
0
. So the answer to
the question “Is
degCent
less than zero?” is
false
, because
degCent
is equal to zero, not less than zero.
The code inside the curly braces will be skipped and never executed. In this case, the next line to be
executed will be the second
if
statement's condition, which we'll discuss shortly.
When you entered
31
in the prompt box,
degFahren
was set to
31
, so the variable
degCent
will be
−
0.55555555556
. So how does your
if
statement look now? It evaluates to “Is
-0.55555555556
less than
zero?” The answer this time is
true
, and the code inside the braces, here just a
document.write()
statement, executes.
Finally, when you entered
212
, how did this alter the
if
statement? The variable
degCent
is set to
100
by the calculation, so the
if
statement now asks the question, “Is 100 less than zero?” The answer is
false
, and the code inside the braces will be skipped over.
In the second
if
statement, you evaluate the condition “Is the value of variable
degCent
equal to 100?”:
if (degCent == 100)
document.write("That's the boiling point of water");
There are no braces here, so if the condition is
true
, the only code to execute is the first line below the
if
statement. When you want to execute multiple lines in the case of the condition being
true
, braces
are required.
You saw that when
degFahren
is
32
,
degCent
will be
0
. So your
if
statement will be “Is 0 equal to
100?” The answer is clearly
false
, and the code won't execute. Again, when you set
degFahren
to
31
,
degCent
will be calculated to be
‐0.55555555556
; “Is
-0.55555555556
equal to 100?” is also
false
, and
the code won't execute.
Finally, when
degFahren
is set to
212
,
degCent
will be
100
. This time the
if
statement is “Is 100 equal
to 100?” and the answer is
true
, so the
document.write()
statement executes.
As you have seen already, one of the most common errors in JavaScript, even for experts, is using one
equals sign for evaluating, rather than the necessary two. Take a look at the following code extract:
if (degCent = 100)
document.write("That's the boiling point of water");
This condition will always evaluate to
true
, and the code below the
if
statement will always execute.
Worse still, your variable
degCent
will be set to
100
. Why? Because a single equals sign assigns values
to a variable; only a double equals sign compares values. The reason an assignment always evaluates to
true
is that the result of the assignment expression is the value of the right‐hand side expression and
this is the number
100
, which is then implicitly converted to a boolean and any number besides
0
and
NaN
converts to
true
.
logical operators
You should have a general idea of how to use conditions in
if
statements now, but how do you use a
condition such as “Is
degFahren
greater than zero but less than 100?” You have two conditions to test
here. You need to test whether
degFahren
is greater than zero
and
whether
degFahren
is less than 100.