Advertisement
Guest User

Untitled

a guest
Jul 24th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. // LOOPS
  2.  
  3. // Loops are ways to run certain lines of code until a
  4. // certain condition is met.
  5. // These conditions are often reaching the end of a predefined sequence.
  6. // To do this loops require creating a variable that
  7. // will explain the length of the loop.
  8. // Each loop has a specific syntax to differentiate itself
  9. // from the others and work in different ways.
  10. // These syntax explain how the variables are defined.
  11.  
  12. // There are several types of loops, including:
  13.  
  14. // FOR
  15. // FOR IN
  16. // WHILE
  17.  
  18. // A FOR loop is an easy way to have code move forward or backward.
  19. // A for loop has three parts to it before you get to its statement.
  20. // * initial expression
  21. // * condition
  22. // * increment expression
  23.  
  24. // Syntax:
  25.  
  26. // for(inintialExpression; condition; incrementExpression)
  27.  
  28. // The INITIAL EXPRESSION is the starting point for your loop.
  29. // This is often represented as a variable "i".
  30. // You must assign this variable a value to start.
  31.  
  32. // The CONDITION tells your code how long it should run.
  33. // Wherever your initial expression starts, this will be its destination.
  34.  
  35. // The INCREMENT EXPRESSION tells your code how it should progress.
  36. // This is where the increment and decrement operators shine.
  37.  
  38. // Example for loop:
  39.  
  40. for(var i = 0; i < 10; i++) {return i;}
  41.  
  42. // In that case, the code would begin at 0 and run until 9.
  43. // It would return each number between 0 and 9.
  44. // You could also go from 9 to 0 by simply swapping the initial expression
  45. // value and condition value, and having the increment expression DEcrement.
  46. // For loops are very useful for things like arrays, but do not
  47. // work on normal objects.
  48. // For example, you could loop through an array using bracket notation.
  49. // In the case of the previous loop, it would be like returning array[i].
  50. // This would not work with an object because objects have keys instead
  51. // of index numbers.
  52.  
  53.  
  54.  
  55. // A FOR IN loop is similar to a for loop, but it can work with objects.
  56. // Its syntax much simpler than a simple for loop.
  57. // You start again by calling for
  58. // Then in parenthesis give a variable to represent the objects properties
  59. // Then say "in"
  60. // Then call the object you want to loop through
  61.  
  62. // Syntax:
  63.  
  64. // for(propertyVariable in object) {code to run}
  65.  
  66. // Example for/in loop:
  67.  
  68. var obj = {key1:1, key2:2, key3:3}
  69. for(i in obj) {return obj[i]} // returns all values of obj
  70.  
  71.  
  72.  
  73. // A WHILE loop will loop WHILE a set condition is still true
  74. // This is similar to a for loop, but with a looser syntax.
  75. // You call WHILE,
  76. // Put a condition in the parenthesis
  77. // The condition should contain a variable that will change
  78. // Variables must be assigned before the loop runs
  79. // Then the code you put in curly braces will run
  80. // as long as the condition is true
  81. // For this loop you must put the increment INSIDE the curly braces.
  82.  
  83. // Syntax:
  84.  
  85. // while(condition) {code to run}
  86.  
  87. // Example of while loop:
  88.  
  89. var n = 10;
  90. while(n > 0) {n--; return n;} // returns 9 through 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement