Looping

1,475 views

Published on

Published in: Technology, News & Politics
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,475
On SlideShare
0
From Embeds
0
Number of Embeds
467
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Looping

  1. 1. loops executing code over and over again Thursday, September 24, 2009
  2. 2. Thursday, September 24, 2009
  3. 3. for loop loop a specified number of times; the only loop you need to know http://www.w3schools.com/jS/js_loop_for.asp Thursday, September 24, 2009
  4. 4. for (var i=0; i < 5; i++) { // some code to run } Thursday, September 24, 2009
  5. 5. before any loops for (var i=0; i < 5; i++) { // some code to run } Thursday, September 24, 2009
  6. 6. before each loop for (var i=0; i < 5; i++) { // some code to run } Thursday, September 24, 2009
  7. 7. after each loop for (var i=0; i < 5; i++) { // some code to run } Thursday, September 24, 2009
  8. 8. for (var i=0; 0 < 5; i++) { // i would be 0 } Thursday, September 24, 2009
  9. 9. for (var i=0; 1 < 5; i++) { // i would be 1 } Thursday, September 24, 2009
  10. 10. for (var i=0; 2 < 5; i++) { // i would be 2 } Thursday, September 24, 2009
  11. 11. for (var i=0; 3 < 5; i++) { // i would be 3 } Thursday, September 24, 2009
  12. 12. for (var i=0; 4 < 5; i++) { // i would be 4 } Thursday, September 24, 2009
  13. 13. for (var i=0; 5 < 5; i++) { // condition false so this // would never happen } Thursday, September 24, 2009
  14. 14. for (var i=0; 5 < 5; i++) { // condition false so this // would never happen } Thursday, September 24, 2009
  15. 15. var colors = ['red', 'green', 'blue']; for(var i=0; i < colors.length; i++) { colors[i]; } Thursday, September 24, 2009
  16. 16. var colors = ['red', 'green', 'blue']; for(var i=0, length=colors.length; i < length; i++) { colors[i]; } Thursday, September 24, 2009
  17. 17. var colors = ['red', 'green', 'blue']; var length = colors.length; for(var i=0; i < length; i++) { colors[i]; } Thursday, September 24, 2009
  18. 18. while loop loop while a condition is true http://www.w3schools.com/JS/js_loop_while.asp Thursday, September 24, 2009
  19. 19. var i = 0; while (i < 5) { // some code to run i++; } Thursday, September 24, 2009
  20. 20. var i = 0; before each loop while (i < 5) { // some code to run i++; } Thursday, September 24, 2009
  21. 21. var i = 0; while (0 < 5) { // i would be 0 i++; // i would be 1 } Thursday, September 24, 2009
  22. 22. var i = 0; while (1 < 5) { // i would be 1 i++; // i would be 2 } Thursday, September 24, 2009
  23. 23. var i = 0; while (2 < 5) { // i would be 2 i++; // i would be 3 } Thursday, September 24, 2009
  24. 24. var i = 0; while (3 < 5) { // i would be 3 i++; // i would be 4 } Thursday, September 24, 2009
  25. 25. var i = 0; while (4 < 5) { // i would be 4 i++; // i would be 5 } Thursday, September 24, 2009
  26. 26. var i = 0; while (5 < 5) { // condition is false so // this would never happen i++; } Thursday, September 24, 2009
  27. 27. var i = 0; while (5 < 5) { // condition is false so // this would never happen i++; } Thursday, September 24, 2009
  28. 28. for..in loop loop through the properties of an object http://www.w3schools.com/jS/js_loop_for_in.asp Thursday, September 24, 2009
  29. 29. for (property in object) { // some code here } Thursday, September 24, 2009
  30. 30. var person = { name: 'John Nunemaker', age: 27, married: true, cat_owner: 'unfortunately' }; for (property in person) { property; person[property]; } Thursday, September 24, 2009
  31. 31. var person = { name: 'John Nunemaker', age: 27, married: true, cat_owner: 'unfortunately' }; for (property in person) { property; // ‘name’ person[property]; // ‘John Nunemaker’ } Thursday, September 24, 2009
  32. 32. var person = { name: 'John Nunemaker', age: 27, married: true, cat_owner: 'unfortunately' }; for (property in person) { property; // ‘age’ person[property]; // 27 } Thursday, September 24, 2009
  33. 33. var person = { name: 'John Nunemaker', age: 27, married: true, cat_owner: 'unfortunately' }; for (property in person) { property; // ‘married’ person[property]; // true } Thursday, September 24, 2009
  34. 34. var person = { name: 'John Nunemaker', age: 27, married: true, cat_owner: 'unfortunately' }; for (property in person) { property; // ‘cat_owner’ person[property]; // ‘unfortunately’ } Thursday, September 24, 2009
  35. 35. break break the current loop and continue with code after loop if any http://www.w3schools.com/jS/js_break.asp Thursday, September 24, 2009
  36. 36. think “stop looping right now” Thursday, September 24, 2009
  37. 37. for (var i=0; i < 5; i++) { if (i === 2) { break; } i; } Thursday, September 24, 2009
  38. 38. for (var i=0; 0 < 5; i++) { if (0 === 2) { // false break; } i; // 0 } Thursday, September 24, 2009
  39. 39. for (var i=0; 0 < 5; i++) { if (0 === 2) { // false break; } i; // 0 } Thursday, September 24, 2009
  40. 40. for (var i=0; 1 < 5; i++) { if (1 === 2) { // false break; } i; // 1 } Thursday, September 24, 2009
  41. 41. for (var i=0; 1 < 5; i++) { if (1 === 2) { // false break; } i; // 1 } Thursday, September 24, 2009
  42. 42. for (var i=0; 2 < 5; i++) { if (2 === 2) { // true break; // breaks out of loop } i; // never gets here } Thursday, September 24, 2009
  43. 43. for (var i=0; 2 < 5; i++) { if (2 === 2) { // true break; // breaks out of loop } i; // never gets here } Thursday, September 24, 2009
  44. 44. most common use is switch statement, but good to know it works elsewhere Thursday, September 24, 2009
  45. 45. continue break the current loop and continue with the next value http://www.w3schools.com/jS/js_break.asp Thursday, September 24, 2009
  46. 46. think “skip current loop” Thursday, September 24, 2009
  47. 47. for (var i=0; i < 3; i++) { if (i === 1) { continue; } i; // comment } Thursday, September 24, 2009
  48. 48. for (var i=0; 0 < 3; i++) { if (0 === 1) { // false continue; } i; // 0 } Thursday, September 24, 2009
  49. 49. for (var i=0; 0 < 3; i++) { if (0 === 1) { // false continue; } i; // 0 } Thursday, September 24, 2009
  50. 50. for (var i=0; 1 < 3; i++) { if (1 === 1) { // true continue; } i; // never gets here } Thursday, September 24, 2009
  51. 51. for (var i=0; 1 < 3; i++) { if (1 === 1) { // true continue; } i; // never gets here } Thursday, September 24, 2009
  52. 52. for (var i=0; 2 < 3; i++) { if (2 === 1) { // false continue; } i; // 2 } Thursday, September 24, 2009
  53. 53. for (var i=0; 2 < 3; i++) { if (2 === 1) { // false continue; } i; // 2 } Thursday, September 24, 2009
  54. 54. for (var i=0; 3 < 3; i++) { // never gets here if (i === 1) { continue; } i; } Thursday, September 24, 2009
  55. 55. for (var i=0; 3 < 3; i++) { // never gets here if (i === 1) { continue; } i; } Thursday, September 24, 2009
  56. 56. assignment04 http://teaching.johnnunemaker.com/capp-30550/sessions/looping/ Thursday, September 24, 2009

×