HTML and CSS Reference
In-Depth Information
Table 14.3
JavaScript Comparison Operators
Operator
Operator
Notes
Description
Equal to
a == b tests to see whether a
equals b .
==
Not equal to
a != b tests to see whether a does
not equal b .
!=
Less than
a < b tests to see whether a is
<
less than b .
Less than or equal to
a <= b tests to see whether a is
less than or equal to b .
<=
Greater than or equal to
a >= b tests = (greater than or
equal to) operator>=)> to see
whether a is greater than or equal
to b .
>=
Greater than
a > b tests (greater than)
operator>)> to see whether a is
greater than b .
>
Loops
You'll occasionally want a group of statements to be executed more than once.
JavaScript supports two kinds of loops. The first, the for loop, is ideal for situations
where you want to execute a group of statements a specific number of times. The second,
the while loop, is useful when you want a set of statements to be executed until a condi-
tion is satisfied.
for Loops Here's a for loop:
for (var count = 1; count <= 10; count++) {
document.write(“Iteration number “ + count + “<br />”);
}
The loop starts with the for keyword, followed by all the information needed to specify
how many times the loop body will be executed. (Trips through a loop are referred to as
iterations .) Three expressions are used to define a for loop. First, a variable is declared
to keep track of the loop iterations. The second is a condition that stops the loop from
executing again when it is false. The third is an expression that increments the loop
counter so that the loop condition will eventually be satisfied. In the preceding example,
I declared the variable count with an initial value of 1 . I specified that the loop will
execute until the value of count is no longer less than or equal to 10. Then I used an
operator you haven't seen, ++ , to increment the value of count by one each time through
the loop.
 
 
Search WWH ::




Custom Search