1 / 50

Looping Dale/Weems/Headington

Looping Dale/Weems/Headington. KA/JS/P Warning. Save your work often! In the Khan Academy, JavaScript environment, infinite loops will lock up the browser. What is a loop?. A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly.

Download Presentation

Looping Dale/Weems/Headington

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. Looping Dale/Weems/Headington

  2. KA/JS/P Warning • Save your work often! • In the Khan Academy, JavaScript environment, infinite loops will lock up the browser.

  3. What is a loop? • A loop is a repetition control structure. • it causes a single statement or block to be executed repeatedly

  4. While Statement SYNTAX while ( Expression) { . . // loop body . } NOTE: Loop body can be a null statement, or a block.

  5. When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE Expression TRUE body statement

  6. Count-controlled loop contains an initialization of the loop control variable an expression to test for continuing the loop an update of the loop controlvariable to be executed with each iteration of the body

  7. Count-controlled Loop var count ; count = 4; // initialize loop variable while (count > 0) // test expression { println(count); // repeated action count --;// update loop variable } println("Done");

  8. count Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT

  9. count 4 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT

  10. count 4 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT

  11. count 4 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4

  12. count 3 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4

  13. count 3 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4

  14. count 3 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3

  15. count 2 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3

  16. count 2 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4 3

  17. count 2 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2

  18. count 1 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2

  19. count 1 Count-controlled Loop var count ; count = 4; while (count > 0) TRUE { println(count); count --; } println("Done"); OUTPUT 4 3 2

  20. count 1 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1

  21. count 0 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1

  22. count 0 Count-controlled Loop var count ; count = 4; while (count > 0) FALSE { println(count); count --; } println("Done"); OUTPUT 4 3 2 1

  23. count 0 Count-controlled Loop var count ; count = 4; while (count > 0) { println(count); count --; } println("Done"); OUTPUT 4 3 2 1 Done

  24. for Statement

  25. A Count-Controlled Loop SYNTAX for ( initialization ; test expression; update ) { 0 or more statements to repeat }

  26. The for loop contains an initialization an expression to test for continuing an update to execute after each iteration of the body

  27. Example of Repetition for ( var num = 1 ; num <= 3; num++) { println(num + " Potato"); }

  28. Example of Repetition ? num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  29. Example of Repetition 1 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  30. Example of Repetition 1 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  31. 1Potato Example of Repetition 1 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  32. 1Potato Example of Repetition 2 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  33. 1Potato Example of Repetition 2 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  34. 1Potato 2Potato Example of Repetition 2 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  35. 1Potato 2Potato Example of Repetition 3 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  36. 1Potato 2Potato Example of Repetition 3 num true var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  37. 1Potato 2Potato 3Potato Example of Repetition 3 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  38. 1Potato 2Potato 3Potato Example of Repetition 4 num var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  39. 1Potato 2Potato 3Potato Example of Repetition 4 num false var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); OUTPUT

  40. Example of Repetition 4 num false var num; for ( num = 1 ; num <= 3; num++) println(num + " Potato"); When the loop control condition is evaluated and has value false, the loop is said to be “satisfied” and control passes to the statement following the For statement.

  41. The output was: 1Potato 2Potato 3Potato

  42. Count-controlled Loop for (var count = 4 ; count > 0 ; count-- ) { println(count); } println(“Done”); OUTPUT: 4 3 2 1 Done

  43. Count Control Loop Example Display integers and their squares from 1 through 10. for (var i = 1; i <= 10; i++) { println(i + " " + i*i); }

  44. For example Display even integers and their squares from 1 through 10. for (var i = 2; i <= 10; i = i+2) { println(i + " " + i*i); }

  45. For example Display integers and their squares from 10 down to 1. for (var i = 10; i >= 1; i--) { println(i + " " + i*i); }

  46. For example Find square roots of 1.1, 1.2, 1.3, ..., 2.0 for (var x = 1.1; x <= 2.0; x =x+0.1) { println(x + " " + sqrt(x)); }

  47. For example Compute and return n! = 1 · 2 · 3 · ... · n. var product = 1; for (var i = 2; i <= n; i++) { product = product * i; }

  48. Nested Loops

  49. Nested Loop Pattern // Initialize outer loop while (Outer loop condition) { : // Initialize inner loop while (Inner loop condition) { // Inner loop processing & update } : // Outer loop update }

  50. To design a nested loop • begin with outer loop • when you get to where the inner loop appears, make it a separate module and come back to its design later • Nested Count Controlled Loops • Use different loop control variables

More Related