13 - JS 3
13 - JS 3
Example
break and continue Statements
In addition to the selection and repetition statements, JavaScript provides the statements break
and continue to alter the flow of control.
We saw earlier how break can be used to terminate a switch statement’s execution.
break Statement
The break statement, when executed in a while, for, do…while or switch statement, causes immediate exit
from the statement. Execution continues with the first statement after the structure.
Example
continue Statement
The continue statement, when executed in a while, for or do…while statement, skips the
remaining statements in the body of the statement and proceeds with the next iteration of the
loop.
Example
Logical Operators
JavaScript provides logical operators that can be used to form more complex conditions by
combining simple conditions.
The logical operators are:
&& (logical AND),
|| (logical OR) and
! (logical NOT, also called logical negation).
&& (Logical AND) Operator
Suppose that, at some point in a program, we wish to ensure that two conditions are both true
before we choose a certain path of execution. In this case, we can use the logical && operator, as
follows:
if ( gender == 1 && age >= 65 )
++seniorFemales;
|| (Logical OR) Operator
Suppose we wish to ensure that either or both of two conditions are true before we choose a
certain path of execution. In this case, we use the || operator, as in the following program
segment:
if ( semesterAverage >= 90 || finalExam >= 90 )
document.writeln( "Student grade is A" );
! (Logical Negation) Operator
JavaScript provides the ! (logical negation) operator to enable you to “reverse” the meaning of a
condition.
Unlike the logical operators && and ||, which combine two conditions the logical negation
operator has only a single condition as an operand.
if ( ! ( grade == sentinelValue ) )
document.writeln( "The next grade is " + grade );
Boolean Equivalents of Nonboolean
Values
Nonzero numeric values are considered to be true.
The numeric value zero is considered to be false.
Any string that contains characters is considered to be true.
The empty string is considered to be false.
The value null and variables that have been declared but not initialized are considered to be
false.
All objects (such as the browser’s document and window objects and JavaScript’s Math object)
are considered to be true.
Operator Precedence and Associativity