Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Looping

 Looping

John Nunemaker

September 24, 2009
Tweet

More Decks by John Nunemaker

Other Decks in Programming

Transcript

  1. 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
  2. for (var i=0; i < 5; i++) { // some

    code to run } Thursday, September 24, 2009
  3. for (var i=0; i < 5; i++) { // some

    code to run } before any loops Thursday, September 24, 2009
  4. for (var i=0; i < 5; i++) { // some

    code to run } before each loop Thursday, September 24, 2009
  5. for (var i=0; i < 5; i++) { // some

    code to run } after each loop Thursday, September 24, 2009
  6. for (var i=0; 0 < 5; i++) { // i

    would be 0 } Thursday, September 24, 2009
  7. for (var i=0; 1 < 5; i++) { // i

    would be 1 } Thursday, September 24, 2009
  8. for (var i=0; 2 < 5; i++) { // i

    would be 2 } Thursday, September 24, 2009
  9. for (var i=0; 3 < 5; i++) { // i

    would be 3 } Thursday, September 24, 2009
  10. for (var i=0; 4 < 5; i++) { // i

    would be 4 } Thursday, September 24, 2009
  11. for (var i=0; 5 < 5; i++) { // condition

    false so this // would never happen } Thursday, September 24, 2009
  12. for (var i=0; 5 < 5; i++) { // condition

    false so this // would never happen } Thursday, September 24, 2009
  13. var colors = ['red', 'green', 'blue']; for(var i=0; i <

    colors.length; i++) { colors[i]; } Thursday, September 24, 2009
  14. var colors = ['red', 'green', 'blue']; for(var i=0, length=colors.length; i

    < length; i++) { colors[i]; } Thursday, September 24, 2009
  15. var colors = ['red', 'green', 'blue']; var length = colors.length;

    for(var i=0; i < length; i++) { colors[i]; } Thursday, September 24, 2009
  16. var i = 0; while (i < 5) { //

    some code to run i++; } Thursday, September 24, 2009
  17. var i = 0; while (i < 5) { //

    some code to run i++; } before each loop Thursday, September 24, 2009
  18. var i = 0; while (0 < 5) { //

    i would be 0 i++; // i would be 1 } Thursday, September 24, 2009
  19. var i = 0; while (1 < 5) { //

    i would be 1 i++; // i would be 2 } Thursday, September 24, 2009
  20. var i = 0; while (2 < 5) { //

    i would be 2 i++; // i would be 3 } Thursday, September 24, 2009
  21. var i = 0; while (3 < 5) { //

    i would be 3 i++; // i would be 4 } Thursday, September 24, 2009
  22. var i = 0; while (4 < 5) { //

    i would be 4 i++; // i would be 5 } Thursday, September 24, 2009
  23. var i = 0; while (5 < 5) { //

    condition is false so // this would never happen i++; } Thursday, September 24, 2009
  24. var i = 0; while (5 < 5) { //

    condition is false so // this would never happen i++; } Thursday, September 24, 2009
  25. for (property in object) { // some code here }

    Thursday, September 24, 2009
  26. var person = { name: 'John Nunemaker', age: 27, married:

    true, cat_owner: 'unfortunately' }; for (property in person) { property; person[property]; } Thursday, September 24, 2009
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. for (var i=0; i < 5; i++) { if (i

    === 2) { break; } i; } Thursday, September 24, 2009
  33. for (var i=0; 0 < 5; i++) { if (0

    === 2) { // false break; } i; // 0 } Thursday, September 24, 2009
  34. for (var i=0; 0 < 5; i++) { if (0

    === 2) { // false break; } i; // 0 } Thursday, September 24, 2009
  35. for (var i=0; 1 < 5; i++) { if (1

    === 2) { // false break; } i; // 1 } Thursday, September 24, 2009
  36. for (var i=0; 1 < 5; i++) { if (1

    === 2) { // false break; } i; // 1 } Thursday, September 24, 2009
  37. for (var i=0; 2 < 5; i++) { if (2

    === 2) { // true break; // breaks out of loop } i; // never gets here } Thursday, September 24, 2009
  38. for (var i=0; 2 < 5; i++) { if (2

    === 2) { // true break; // breaks out of loop } i; // never gets here } Thursday, September 24, 2009
  39. most common use is switch statement, but good to know

    it works elsewhere Thursday, September 24, 2009
  40. continue break the current loop and continue with the next

    value http://www.w3schools.com/jS/js_break.asp Thursday, September 24, 2009
  41. for (var i=0; i < 3; i++) { if (i

    === 1) { continue; } i; // comment } Thursday, September 24, 2009
  42. for (var i=0; 0 < 3; i++) { if (0

    === 1) { // false continue; } i; // 0 } Thursday, September 24, 2009
  43. for (var i=0; 0 < 3; i++) { if (0

    === 1) { // false continue; } i; // 0 } Thursday, September 24, 2009
  44. for (var i=0; 1 < 3; i++) { if (1

    === 1) { // true continue; } i; // never gets here } Thursday, September 24, 2009
  45. for (var i=0; 1 < 3; i++) { if (1

    === 1) { // true continue; } i; // never gets here } Thursday, September 24, 2009
  46. for (var i=0; 2 < 3; i++) { if (2

    === 1) { // false continue; } i; // 2 } Thursday, September 24, 2009
  47. for (var i=0; 2 < 3; i++) { if (2

    === 1) { // false continue; } i; // 2 } Thursday, September 24, 2009
  48. for (var i=0; 3 < 3; i++) { // never

    gets here if (i === 1) { continue; } i; } Thursday, September 24, 2009
  49. for (var i=0; 3 < 3; i++) { // never

    gets here if (i === 1) { continue; } i; } Thursday, September 24, 2009