Java Reference
In-Depth Information
The third if statement asks, “Is myAge greater than or equal to 80, or less than or equal to 10?” Taking
the LHS condition fi rst — ”Is 30 greater than or equal to 80?” — the answer is false. The answer to the
RHS condition — ”Is 30 less than or equal to 10?” — is again false. These two halves of the condition
are combined using ||, which indicates the OR operator. Looking at the OR result table earlier in this
section, you see that false OR false produces a result of false. So again the if statement's condition
evaluates to false, and the code within the curly braces does not execute.
The fi nal if statement is a little more complex.
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) )
{
document.write(“myAge is between 30 and 39 “ +
“or myAge is between 80 and 89<br />”);
}
It asks the question, “Is myAge between 30 and 39 or between 80 and 89?” Let's break the condition
down into its component parts. There is a left-hand-side and a right-hand-side condition, combined by
means of an OR operator. However, the LHS and RHS themselves have an LHS and RHS each, which
are combined using AND operators. Notice how parentheses are used to tell JavaScript which parts of
the condition to evaluate fi rst, just as you would do with numbers in a mathematical calculation.
Let's look at the LHS of the condition fi rst, namely (myAge >= 30 && myAge <= 39) . By putting the condi-
tion into parentheses, you ensure that it's treated as a single condition; no matter how many conditions
are inside the parentheses, it only produces a single result, either true or false . Breaking down the
conditions in the parentheses, you have “Is 30 greater than or equal to 30?” with a result of true , and
“Is 30 less than or equal to 39?” again with a result of true . From the AND table, you know true AND
true produces a result of true .
Now let's look at the RHS of the condition, namely (myAge >= 80 && myAge <= 89). Again breaking the
condition down, you see that the LHS asks, “Is 30 greater than or equal to 80?” which gives a false
result, and the RHS asks, “Is 30 less than or equal to 89?” which gives a true result. You know that
false AND true gives a false result.
Now you can think of your if statement's condition as looking like (true || false) . Looking at the
OR results table, you can see that true OR false gives a result of true , so the code within the braces
following the if statement will execute, and a line will be written to the page.
However, remember that JavaScript does not evaluate conditions where they won't affect the fi nal
result, and the preceding condition is one of those situations. The LHS of the condition evaluated to
true. After that, it does not matter if the RHS of the condition is true or false because only one of the
conditions in an OR operation needs to be true for a result of true. Thus JavaScript does not actually
evaluate the RHS of the condition. We did so simply for demonstration purposes.
As you have seen, the easiest way to approach understanding or creating multiple conditions is to break
them down into the smallest logical chunks. You'll fi nd that with experience, you will do this almost
without thinking, unless you have a particularly tricky condition to evaluate.
Although using multiple conditions is often better than using multiple if statements, there are times
when it makes your code harder to read and therefore harder to understand and debug. It's possible to
have 10, 20, or more than 100 conditions inside your if statement, but can you imagine trying to read an
if statement with even 10 conditions? If you feel that your multiple conditions are getting too complex,
break them down into smaller logical chunks.
Search WWH ::




Custom Search