What four things does counter-controlled repetition require?
The name of a control variable; its initial value; the increment (or decrement) by which it is modified each time through the loop; the condition that tests for its final value to determine whether looping should continue.
How do you put a double-quote character inside a JavaScript string literal?
Precede it with a \ to create the escape sequence \" (the double quote otherwise delimits the beginning and end of the string literal).
Which quotes may surround an HTML5 attribute value? Can a JavaScript string literal contain single quotes?
HTML5 allows either single quotes (') or double quotes ("). JavaScript allows single quotes to be placed in a string literal.
What three expressions does a for statement header contain?
Initialization; condition; increment expression.
What can cause an off-by-one error in a loop?
Using a < or > instead of a <= or >= in the loop's condition, or vice-versa.
How does the increment expression of a for statement behave?
It acts like a stand-alone statement at the end of the body of the for statement.
In a for statement, which parts are optional and which are required?
The three expressions are optional; the two semicolons are required.
What is a variable's scope?
The part of a script in which a variable name can be used.
What happens if the loop-continuation condition of a for is false at the start?
The body of the for statement is not performed; execution proceeds with the statement following the for statement.
What is a negative "increment" in a for statement called?
A decrement: the loop actually counts downward.
What does the loop for ( var number = 2; number <= 100; number += 2 ) sum += number; compute, and what is the result?
The sum of the even integers from 2 to 100: 2550.
How do you compute x raised to the y th power in JavaScript?
Math.pow(x, y) — JavaScript does not include an exponentiation operator.
Compound interest (principal 1000.00, rate 0.05, years 1–10): which statement computes each year's amount?
amount = principal * Math.pow( 1.0 + rate, year ); with principal = 1000.00 and rate = 0.05, for year 1 to 10.
What does the switch multiple-selection statement do?
It tests a variable or expression separately for each of the values it may assume; different actions are taken for each value.
Name the list-style-type values.
decimal (numbers — the default), lower-roman, upper-roman, lower-alpha, upper-alpha, others.
What does a switch statement do when control reaches it?
It evaluates the controlling expression in the parentheses and compares this value with the value in each case label; if a comparison is true, the statements after that case label execute in order until a break statement is reached.
What is the default case of a switch for?
To specify a set of statements to execute if no other case is satisfied; usually the last case in the switch statement.
Why is break not required for the last case of a switch?
Because program control automatically continues with the next statement after the switch.
What happens when several case labels are listed together with no statements between them?
The same set of actions executes for each of those cases (e.g., case 1: case 2:).
Are braces required around multiple actions in a case?
No. Each case can have multiple actions, and braces are not required around them.
When does do…while test its condition, and what follows from that?
After the loop body executes, so the loop body always executes at least once.
What does break do in a while, for, do…while or switch?
Causes immediate exit from the statement; execution continues with the next statement in sequence.
Two common uses of break.
Escape early from a loop; skip the remainder of a switch statement.
What does continue do in a while, for or do…while?
Skips the remaining statements in the body and proceeds with the next iteration of the loop.
After continue: what happens next in a for loop, compared with while / do…while?
In for statements, the increment expression executes, then the loop-continuation test evaluates. In while and do…while, the loop-continuation test evaluates immediately.
Name JavaScript's three logical operators.
&& (logical AND), || (logical OR), ! (logical NOT, also called logical negation).
When is expression1 && expression2 true?
Only when both conditions are true (Fig. 8.13).
When is expression1 || expression2 true?
When either or both of the conditions are true; false only when both are false (Fig. 8.14).
Precedence and associativity of && and ||.
&& has a higher precedence than ||. Both associate from left to right.
What is short-circuit evaluation?
An expression containing && or || operators is evaluated only until truth or falsity is known.
Why is ! a unary operator?
It has only a single condition as an operand; it reverses the meaning of that condition.
Which nonboolean values are considered false?
The numeric value zero; the empty string; the value null; variables that have been declared but not initialized. (Nonzero numbers, strings that contain characters and all objects are true.)
Fig. 8.16: which operator groups associate right to left?
The unary operators ++ -- !, the conditional ?:, and the assignment operators = += -= *= /= %=. All the others listed associate left to right.