You are on page 1of 21

JS

Essentials of Counter-Controlled Repetition


Counter-controlled repetition requires:
1. The name of a control variable (or loop counter).
2. The initial value of the control variable.
3. The increment (or decrement) value.
4. The condition that tests for the final value of the control variable.
General Format of a for Statement
for ( initialization; loopContinuationTest; increment )
statements
General Format of a for Statement
The three expressions in the for statement’s header are optional.
If loopContinuationTest is omitted, we’ll have an infinite loop.
One might omit the initialization expression if the control variable is initialized before the loop.
One might omit the increment expression if the increment is calculated by statements in
theloop’s body or if no increment is needed.
Example
Flowchart of a for loop
Example
var sum = 0;
for ( var number = 2; number <= 100; number += 2 )
sum += number;

Is equivalent to the following:


for ( var number = 2; number <= 100; sum += number, number += 2)
;
The comma operator, which guarantees that the expression to its left is evaluated before the expression to
its right.
Example
Consider the following problem statement:
A person invests $1000.00 in a savings account yielding 5 percent interest. Assuming that all the interest is left
on deposit, calculate and print the amount of money in the account at the end of each year for 10 years. Use the
following formula to determine the amounts:
a = p (1 + r) n
where
p is the original amount invested (i.e., the principal)
r is the annual interest rate
n is the number of years
a is the amount on deposit at the end of the nth year.
Trace the Code
switch Multiple-Selection Statement
Flowcharting the Switch Statement
do…while Repetition Statement
In the while statement, the loop-continuation test occurs at the beginning of the loop, before the
body of the loop executes.
The do…while statement tests the loop-continuation condition after the loop body executes—
therefore, the loop body always executes at least once.
do…while Repetition Statement

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

You might also like