Java Reference
In-Depth Information
It asks the question, “Is
myAge
between 30 and 39 or between 80 and 89?” Let's break down the
condition 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 first, just as you would do with numbers in a mathematical calculation.
Let's look at the LHS of the condition first, namely
(myAge >= 30 && myAge <= 39)
. By putting the
condition 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
down the condition, 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 final
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 find 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, sometimes 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.
For example, imagine you want to execute some code if
myAge
is in the ranges 30-39, 80-89, or
100-115, using different code in each case. You could write the statement like so:
if ( (myAge >= 30 && myAge <= 39) || (myAge >= 80 && myAge <= 89) ||
(myAge >= 100 && myAge <= 115) ) {
document.write("myAge is between 30 and 39 " +
"or myAge is between 80 " +
"and 89 or myAge is between 100 and 115");
}
There's nothing wrong with this, but it is starting to get a little long and difficult to read. Instead, you
could create another
if
statement for the code executed for the 100-115 range.