HTML and CSS Reference
In-Depth Information
Figure 10-7 shows the flowchart that corresponds to an if statement. The if statement
determines if the current value of the beginPos variable is greater than the length of
the message.
Spaces Between
Operators and
Operands
Spaces between an
operand and operator are
optional. It may be easier
to read an expression if
spaces are used, but they
are not necessary.
Table 10-4 if Statement
General form:
if (condition) {
JavaScript statements if condition true
}
Comment:
where condition is the comparison of values. All conditions must be placed in parentheses.
If the result of the comparison is true, JavaScript executes the statements between the curly
braces. If the result of the comparison is false, the JavaScript statements after the closing
brace are executed.
Example:
if (beginPos>adMsg.length) {
beginPos=0
}
As shown in the example in Table 10-4, the conditions use symbols called operators
to indicate what type of comparisons should be made between the values. Table 10-5 shows
the conditional operators used for comparisons. For more information about conditional
operators, see the JavaScript Quick Reference in Appendix G.
comparison
of values
JavaScript
statements that
are executed if
condition is True
Figure 10-7
Conditional Operators
Conditional operators
are symbols used to
compare two values,
called operands. The
operator compares the
two operands and returns
a logical value based on
whether the comparison
is true.
Table 10-5 Conditional Operators
Operators
Example
Results
==
(a==b)
True if a equals b
===
(a=== b)
True if a equals b and the data are of the same type
!=
(a!=b)
True if a does not equal b
!==
(a!==b)
True if a does not equal b and/or the data are not of the same type
>
(a>b)
True if a is greater than b
<
(a<b)
True if a is less than b
>=
(a>=b)
True if a is greater than or equal to b
<=
(a<=b)
True if a is less than or equal to b
&&
(a==b) && (x<y)
True if both conditions are true (a equals b and x is less than y)
||
(a!=b) || (x>=a)
True if either condition is true (a does not equal b or x is greater than or
equal to a)
!
!(a<b)
True if a is greater than b; False if a is less than b
The flowchart and sample code shown in Figure 10-8 illustrate how the if statement
compares the beginning position variable (beginPos) with the overall length of the message
(adMsg.length).
 
Search WWH ::




Custom Search