Java Reference
In-Depth Information
Just change the
break
statement to a
continue
. You will still get a message about the invalid data,
but the third value will also be converted.
summarY
In this chapter you continued your look at the core of the JavaScript language and its syntax.
The chapter looked at the following:
➤
Decision making with the
if
and
switch
statements:
The ability to make decisions is essen-
tially what gives the code its “intelligence.” Based on whether a condition is
true
or
false
,
you can decide on a course of action to follow.
➤
Comparison operators:
The comparison operators compare the value on the left of the opera-
tor (left‐hand side, LHS) with the value on the right of the operator (right‐hand side, RHS)
and return a boolean value. Here is a list of the main comparison operators:
➤
==
means “is the LHS equal to the RHS?”
➤
!=
means “is the LHS not equal to the RHS?”
➤
<=
means “is the LHS less than or equal to the RHS?”
➤
>=
means “is the LHS greater than or equal to the RHS?”
➤
<
means “is the LHS less than the RHS?”
➤
>
means “is the LHS greater than the RHS?”
➤
The
if
statement:
Using the
if
statement, you can choose to execute a block of code (defined
by being in curly braces) when a condition is
true
. The
if
statement has a test condition,
specified in parentheses. If this condition evaluates to
true
, the code after the
if
statement
will execute.
➤
The
else
statement:
If you want code to execute when the
if
statement is
false
, you can
use the
else
statement that appears after the
if
statement.
➤
Logical operators:
To combine conditions, you can use the three logical operators:
AND
,
OR
,
and
NOT
, represented by
&&
,
||
, and
!
, respectively:
➤
The
AND
operator returns
true
only if both sides of the expression are
true
.
➤
The
OR
operator returns
true
when either one or both sides of an expression are
true
.
➤
The
NOT
operator reverses the logic of an expression.
➤
The
switch
statement:
This compares the result of an expression with a series of possible
cases and is similar in effect to a multiple
if
statement.
➤
Looping with
for
,
for . . . in
,
while
,
and
do . . . while
:
It's often necessary to repeat a block
of code a number of times, something JavaScript enables by looping.
➤
The
for
loop:
Useful for looping through code a certain number of times, the
for
loop consists of three parts: the initialization, test condition, and increment parts.