380 likes | 395 Views
Loops. George Mason University. 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
E N D
Loops George Mason University
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
Repetitions for Loops while Loops do-while Loops break and continue
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 Loops for (i = 0; i < 100; i++) { document.write( "Welcome to JavaScript!<br />"); } for (initial-action; loop-continuation-condition; action-after-each-iteration) { // loop body; statement(s); } i=0 initial-action i<100 loop-continuation-condition true true statement(s); print(“welcome”) false false action-after- each-iteration i++
Trace for Loop for (i = 0; i < 2; i++) { document.write( "Welcome to JavaScript!<br />"); } ???? MEMORY i=0 i WEB PAGE i<2 true print(“welcome”) false i++
Trace for Loop, cont. Execute initializer i is now 0 for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } ???? 0 MEMORY i=0 i WEB PAGE i<2 true print(“welcome”) false i++
Trace for Loop, cont. (i < 2) is true since i is 0 for (i = 0; i < 2; i++) { document.write( "Welcome to JavaScript!<br />"); } 0 MEMORY i=0 i WEB PAGE i<2 true print(“welcome”) false i++
Trace for Loop, cont. Print Welcome to JavaScript int i; for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript !<br />"); } 0 MEMORY i=0 int i WEB PAGE Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. Execute adjustment statement i now is 1 for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } 0 1 MEMORY i=0 i CONSOLE Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. (i < 2) is still true since i is 1 for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } 1 MEMORY i=0 i WEB PAGE Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. Print Welcome to JavaScript for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } 1 MEMORY i=0 i CONSOLE Welcome to Java! Welcome to JavaScript! Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. Execute adjustment statement i now is 2 for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } 1 2 MEMORY i=0 i WEB PAGE Welcome to JavaScript! Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. (i < 2) is false since i is 2 for (i = 0; i < 2; i++) { document.write("Welcome to JavaScript!<br />"); } 2 MEMORY i=0 i WEB PAGE Welcome to JavaScript! Welcome to JavaScript! i<2 true print(“welcome”) false i++
Trace for Loop, cont. Exit the loop. Execute the next statement after the loop for (i = 0; i < 2; i++) { dcoument.write("Welcome to JavaScript!<br />"); } 2 MEMORY i=0 i CONSOLE Welcome to JavaScript! Welcome to JavaScript! i<2 true print(“welcome”) false i++
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 count = 0; while (count < 100) { document.write("Welcome to JavaScript!<br />"); count++; } while (loop-continuation-condition) { // loop-body; Statement(s); }
Trace while Loop Initialize count count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 0 MEMORY count WEB PAGE
Trace while Loop, cont. (count < 2) is true count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 0 MEMORY count WEB PAGE
Trace while Loop, cont. Print Welcome to JavaScript count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 0 MEMORY count WEB PAGE Welcome to JavaScript!
Trace while Loop, cont. count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } Increase count by 1 count is 1 now 0 1 MEMORY count WEB PAGE Welcome to JavaScript!
Trace while Loop, cont. (count < 2) is still true since count is 1 count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 1 MEMORY count WEB PAGE Welcome to JavaScript!
Trace while Loop, cont. Print Welcome to JavaScript count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 1 MEMORY count CONSOLE Welcome to Java! 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 1 count is 2 now 1 2 MEMORY int count CONSOLE Welcome to JavaScript! Welcome to JavaScript!
Trace while Loop, cont. (count < 2) is false since count is 2 now count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 2 MEMORY count CONSOLE Welcome to JavaScript! Welcome to JavaScript!
Trace while Loop The loop exits. Execute the next statement after the loop. count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 2 MEMORY count WEB PAGE Welcome to JavaScript! Welcome to JavaScript!
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);
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
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
Use of break and continue Used to alter the intended flow of execution • break; //end the control structure • continue; //end current iteration