loops george mason university. loop structure loop- a structure that allows repeated execution of a...
TRANSCRIPT
Loop Structure
Loop- A structure that allows repeated execution of a block of statements
Loop body- A block of statements; as long as the expression is true, the loop body executes
Iteration- One execution of any loop
Loop Choices
Definite Loop• #iterations known• for loop
Indefinite Loop• #iterations unknown (conditional)
– while loop – condition checked at top
– do/while loop – condition checked at bottom will always execute at least once
Using a for Loop
for loop- A special loop that is used when a definite number of loop iterations is required
Keyword for
Set of parentheses
Three sections within parentheses• Initializing the loop control variable
• Testing the loop control variable
• Updating the loop control variable
for Loopsfor (initial-action; loop-
continuation-condition; action-after-each-iteration) {
// loop body; statement(s);}
for (i = 0; i < 100; i++) { document.write( "Welcome to JavaScript!
<br />"); }
initial-action
loop-continuation-condition
true
false
action-after-each-iteration
statement(s);
i=0
i<100true
false
i++
print(“welcome”)
Trace for Loop
for (i = 0; i < 2; i++) { document.write( "Welcome to JavaScript!<br />"); }
i=0
i<2true
false
i++
print(“welcome”)
????????
i
MEMORY
WEB PAGE
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
Execute initializeri is now 0
i=0
i<2true
false
i++
print(“welcome”)
????????
i
MEMORY
WEB PAGE
00
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write( "Welcome to JavaScript!<br />"); }
(i < 2) is true since i is 0
i=0
i<2true
false
i++
print(“welcome”)
00
i
MEMORY
WEB PAGE
Trace for Loop, cont.
int i;for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript !<br />"); }
Print Welcome to JavaScript
i=0
i<2true
false
i++
print(“welcome”)
00
int i
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
Execute adjustment statement i now is 1
i=0
i<2true
false
i++
print(“welcome”)
00
i
MEMORY
CONSOLE
Welcome to JavaScript!Welcome to JavaScript!
11
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
(i < 2) is still true since i is 1
i=0
i<2true
false
i++
print(“welcome”)
11
i
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
Print Welcome to JavaScript
i=0
i<2true
false
i++
print(“welcome”)
11
i
MEMORY
CONSOLE
Welcome to Java!Welcome to Java!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
Execute adjustment statement i now is 2
i=0
i<2true
false
i++
print(“welcome”)
11
i
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
22
Trace for Loop, cont.
for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); }
(i < 2) is false since i is 2
i=0
i<2true
false
i++
print(“welcome”)
22
i
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
Trace for Loop, cont.
for (i = 0; i < 2; i++) { dcoument.write("Welcome to JavaScript!<br />"); }
Exit the loop. Execute the next statement after the loop
i=0
i<2true
false
i++
print(“welcome”)
22
i
MEMORY
CONSOLE
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
Nested For Loops
• for loops can be nested, e.g.
for (row=1; row<=9; row++)
for (column=1; column<=9; column++)
document.write(row*column + " ");
The above example would write out 81 numbers, going row by row and then by column within a row
Indefinite Loop
Don’t always have access to the number of iterations ahead of time
If a condition (user-response, limit reached, …) controls the number of iterations – indefinite loop is appropriate
Using the while Loop
while Loop- To execute a body of statements continually as long as the Boolean expression continues to be true• Consists of the keyword while followed by a
Boolean expression within parentheses followed by the body of the loop
• Use when you need to perform a task a undetermined number of times
while Loop
while (loop-continuation-condition) {
// loop-body;
Statement(s);
}
count = 0;
while (count < 100) {
document.write("Welcome to JavaScript!<br />");
count++;
}
Loop Continuation Condition?
true
Statement(s) (loop body)
false (count < 100)?
true
document.write("Welcome to JavaScript!<br />"); count++;
false
(A) (B)
count = 0;
Trace while Loop
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
Initialize count
00
count
MEMORY
WEB PAGE
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
(count < 2) is true
00
count
MEMORY
WEB PAGE
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
Print Welcome to JavaScript
00
count
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
Increase count by 1count is 1 now
00
count
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!
11
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
(count < 2) is still true since count is 1
count
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!
11
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
Print Welcome to JavaScript
count
MEMORY
CONSOLE
Welcome to Java!Welcome to Java!
11
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript<br />!");
count++;
}
Increase count by 1count is 2 now
int count
MEMORY
CONSOLE
11
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
22
Trace while Loop, cont.
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
(count < 2) is false since count is 2 now
count
MEMORY
CONSOLE
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
22
Trace while Loop
count = 0;
while (count < 2) {
document.write("Welcome to JavaScript!<br />");
count++;
}
The loop exits. Execute the next statement after the loop.
count
MEMORY
WEB PAGE
Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!Welcome to JavaScript!
22
Ending a Loop with a Sentinel Value Often the number of times a loop is executed is not predetermined. You may use an input value to signify the end of the loop. Such a value is known as a sentinel value.
do…while loop
Checks at the bottom of the loop after one repetition has occurred
Loop body executes at least one time
The loop starts with the keyword do
The body of the loop is contained within curly braces
do-while Loop
do {
// Loop body;
Statement(s);
} while (loop-continuation-condition);
Loop Continuation Condition?
true
Statement(s) (loop body)
false
Which Loop to Use?The three forms of loop statements, while, do-while, and for, are expressively equivalent; that is, you can write a loop in any of these three forms. For example, a while loop in (a) in the following figure can always be converted into the following for loop in (b):
A for loop in (a) in the following figure can generally be converted into the following while loop in (b) except in certain special cases
for (initial-action; loop-continuation-condition; action-after-each-iteration) { // Loop body; }
(a)
Equivalent
(b)
initial-action; while (loop-continuation-condition) { // Loop body; action-after-each-iteration; }
while (loop-continuation-condition) { // Loop body }
(a)
Equivalent
(b)
for ( ; loop-continuation-condition; ) { // Loop body }
Which Loop to Use?
Interchangeable – BUT, only one is the right choice!• Definite: for loop• Indefinite and at least one iteration: do/while• Indefinite and not necessarily at least one
iteration: while
Common Logic Error
Semicolon to terminate loops (for/while) prior to the body!
for (i=0; i<10; i++);
document.write("<br />i is " + i);
---------------------------------------------------
play = prompt(“Play Game? Enter y for yes, n for no");
while (play == "y"); // play Scrabble
Caution, cont.
Similarly, the following loop is also wrong: i=0; while (i < 10);{ document.write("<br />i is " + i); i++;}
In the case of the do loop, the following semicolon is needed to end the loop.
i=0; do { document.write("<br />i is " + i); i++;} while (i<10);
Logic Error
Correct