Web Programming Ch. 8
Web Programming Ch. 8
The double-quote character delimits the beginning and end of a string literal in JavaScript
it cannot be used in a string unless it is preceded by a \ to create the escape sequence \
HTML5 allows either single quotes (') or double quotes (") to be placed around the value specified for an attribute JavaScript allows single quotes to be placed in a string literal
for statement
If the loops condition uses a < or > instead of a <= or >=, or vice-versa, it can result in an off-by-one error for statement header contains three expressions
Initialization Condition Increment Expression
Specifies each of the items needed for counter-controlled repetition with a control variable Can use a block to put multiple statements into the body
The increment expression in the for statement acts like a stand-alone statement at the end of the body of the for statement Place only expressions involving the control variable in the initialization and increment sections of a for statement
The three expressions in the for statement are optional The two semicolons in the for statement are required The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions
The part of a script in which a variable name can be used is known as the variables scope The increment of a for statement may be negative, in which case it is called a decrement and the loop actually counts downward If the loop-continuation condition initially is false, the body of the for statement is not performed
Execution proceeds with the statement following the for statement
Figure 8.5 uses the for statement to sum the even integers from 2 to 100.
switch statement
Consists of a series of case labels and an optional default case When control reaches a switch statement
The break statement is used as the last statement in each case to exit the switch statement immediately The default case allows you to specify a set of statements to execute if no other case is satisfied
Usually the last case in the switch statement
The script evaluates the controlling expression in the parentheses Compares this value with the value in each of the case labels If the comparison evaluates to true, the statements after the case label are executed in order until a break statement is reached
Each case can have multiple actions (statements) Braces are not required around multiple actions in a case of a switch The break statement is not required for the last case because program control automatically continues with the next statement after the switch Having several case labels listed together (e.g., case 1: case 2: with no statements between the cases) executes the same set of actions for each case
dowhile statement
tests the loop-continuation condition after the loop body executes The loop body always executes at least once
Logical operators can be used to form complex conditions by combining simple conditions
&& (logical AND) || (logical OR) ! (logical NOT, also called logical negation)
The && operator is used to ensure that two conditions are both true before choosing a certain path of execution JavaScript evaluates to false or true all expressions that include relational operators, equality operators and/or logical operators
The || (logical OR) operator is used to ensure that either or both of two conditions are true before choosing choose a certain path of execution
The && operator has a higher precedence than the || operator Both operators associate from left to right. An expression containing && or || operators is evaluated only until truth or falsity is known
This is called short-circuit evaluation
Most nonboolean values can be converted to a boolean true or false value 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 are considered to be true
1992-2012 by Pearson Education, Inc. All Rights Reserved.