1 / 38

Loops

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

fburgess
Download Presentation

Loops

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops George Mason University

  2. 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

  3. 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

  4. Repetitions for Loops while Loops do-while Loops break and continue

  5. 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

  6. 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++

  7. 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++

  8. 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++

  9. 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++

  10. 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++

  11. 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++

  12. 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++

  13. 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++

  14. 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++

  15. 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++

  16. 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++

  17. 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

  18. 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

  19. While Loop

  20. 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

  21. while Loop count = 0; while (count < 100) { document.write("Welcome to JavaScript!<br />"); count++; } while (loop-continuation-condition) { // loop-body; Statement(s); }

  22. Trace while Loop Initialize count count = 0; while (count < 2) { document.write("Welcome to JavaScript!<br />"); count++; } 0 MEMORY count WEB PAGE

  23. 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

  24. 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!

  25. 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!

  26. 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!

  27. 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!

  28. 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!

  29. 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!

  30. 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!

  31. 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.

  32. 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

  33. do-while Loop do { // Loop body; Statement(s); } while (loop-continuation-condition);

  34. 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

  35. 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

  36. 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

  37. 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

  38. Use of break and continue Used to alter the intended flow of execution • break; //end the control structure • continue; //end current iteration

More Related