Java Reference
In-Depth Information
Now comes the new code; the fi rst of two if statements.
if (degCent < 0)
{
document.write(“That's below the freezing point of water”);
}
This if statement has the condition that asks, “Is the value of the variable degCent less than zero?” If
the answer is yes ( true ), the code inside the curly braces executes. In this case, you write a sentence to
the page using document.write() . If the answer is no ( false ), the processing moves on to the next
line after the closing brace. Also worth noting is the fact that the code inside the if statement's opening
brace is indented. This is not necessary, but it is a good practice to get into because it makes your code
much easier to read.
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 fi rst 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
Search WWH ::




Custom Search