JS Control Statements II
JS Control Statements II
Statements II
Outline
9.1 Introduction
9.2 Essentials of Counter-Controlled Repetition
9.3 for Repetition Statement
9.4 Examples Using the for Statement
9.5 switch Multiple-Selection Statement
9.6 do…while Repetition Statement
9.7 break and continue Statements
9.8 Labeled break and continue Statements
9.9 Logical Operators
9.10 Summary of Structured Programming
9.11 Web Resources
• Continuation of Chapter 8
– Theory and principles of structured programming
WhileCounter.html
(2 of 2)
Establish
initial value
of control
variable.
var counter = 1
document.writeln(
true "<p style=\"font-size: "
counter <= 7 ++counter
+ counter +
Increment
"ex\">XHTML font size " +
counter + "ex</p>" ); the control
false variable.
Body of loop
Determine
(this may be many
if final value
statements)
of control
variable
has been
reached.
• Controlling expression
• Case labels
• Default case
true
case a case a action(s) break
false
true
case b case b action(s) break
false
.
.
.
true
case z case z action(s) break
false
default action(s)
DoWhileTest.html
(2 of 2)
action(s)
true
condition
false
• break
– Immediate exit from the structure
– Used to escape early from a loop
– Skip the remainder of a switch statement
• continue
– Skips the remaining statements in the body of the structure
– Proceeds with the next iteration of the loop
expression1 expression2
expression1 ||
expression2
false false false
false true true
true false true
true true true
Fig. 9.16 Truth table for the || (logical OR) operator.
expression !expression
false true
true false
Fig. 9.17 Truth table for operator ! (logical negation).
• Flowcharts
– Reveal the structured nature of programs
• Single-entry/single-exit control structures
– Only one way to enter and one way to exit each control
structure
• Control structure stacking
– The exit point of one control structure is connected to the
entry point of the next control structure
do…while statement
while statement
T
Repetition
F
for statement
T
T
F
Fig. 9.20 Single-entry/single-exit sequence, selection and repetition structures. (1 of 3)
if…else statement
(double selection)
T
break
break
break
Selection
(multiple selection)
switchstatement
(single selection)
if statement
T
F
F
.
.
.
T
.
.
Fig. 9.20
.
Single-entry/single-exit sequence, selection and repetition structures. (3 of 3)
.
.
.
Fig. 9.23 Repeatedly applying rule 2 of Fig. 9.21 to the simplest flowchart.
Rule 3
Rule 3