Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- // LOOPS
- // Loops are ways to run certain lines of code until a
- // certain condition is met.
- // These conditions are often reaching the end of a predefined sequence.
- // To do this loops require creating a variable that
- // will explain the length of the loop.
- // Each loop has a specific syntax to differentiate itself
- // from the others and work in different ways.
- // These syntax explain how the variables are defined.
- // There are several types of loops, including:
- // FOR
- // FOR IN
- // WHILE
- // A FOR loop is an easy way to have code move forward or backward.
- // A for loop has three parts to it before you get to its statement.
- // * initial expression
- // * condition
- // * increment expression
- // Syntax:
- // for(inintialExpression; condition; incrementExpression)
- // The INITIAL EXPRESSION is the starting point for your loop.
- // This is often represented as a variable "i".
- // You must assign this variable a value to start.
- // The CONDITION tells your code how long it should run.
- // Wherever your initial expression starts, this will be its destination.
- // The INCREMENT EXPRESSION tells your code how it should progress.
- // This is where the increment and decrement operators shine.
- // Example for loop:
- for(var i = 0; i < 10; i++) {return i;}
- // In that case, the code would begin at 0 and run until 9.
- // It would return each number between 0 and 9.
- // You could also go from 9 to 0 by simply swapping the initial expression
- // value and condition value, and having the increment expression DEcrement.
- // For loops are very useful for things like arrays, but do not
- // work on normal objects.
- // For example, you could loop through an array using bracket notation.
- // In the case of the previous loop, it would be like returning array[i].
- // This would not work with an object because objects have keys instead
- // of index numbers.
- // A FOR IN loop is similar to a for loop, but it can work with objects.
- // Its syntax much simpler than a simple for loop.
- // You start again by calling for
- // Then in parenthesis give a variable to represent the objects properties
- // Then say "in"
- // Then call the object you want to loop through
- // Syntax:
- // for(propertyVariable in object) {code to run}
- // Example for/in loop:
- var obj = {key1:1, key2:2, key3:3}
- for(i in obj) {return obj[i]} // returns all values of obj
- // A WHILE loop will loop WHILE a set condition is still true
- // This is similar to a for loop, but with a looser syntax.
- // You call WHILE,
- // Put a condition in the parenthesis
- // The condition should contain a variable that will change
- // Variables must be assigned before the loop runs
- // Then the code you put in curly braces will run
- // as long as the condition is true
- // For this loop you must put the increment INSIDE the curly braces.
- // Syntax:
- // while(condition) {code to run}
- // Example of while loop:
- var n = 10;
- while(n > 0) {n--; return n;} // returns 9 through 0
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">// LOOPS
- // Loops are ways to run certain lines of code until a
- // certain condition is met.
- // These conditions are often reaching the end of a predefined sequence.
- // To do this loops require creating a variable that
- // will explain the length of the loop.
- // Each loop has a specific syntax to differentiate itself
- // from the others and work in different ways.
- // These syntax explain how the variables are defined.
- // There are several types of loops, including:
- // FOR
- // FOR IN
- // WHILE
- // A FOR loop is an easy way to have code move forward or backward.
- // A for loop has three parts to it before you get to its statement.
- // * initial expression
- // * condition
- // * increment expression
- // Syntax:
- // for(inintialExpression; condition; incrementExpression)
- // The INITIAL EXPRESSION is the starting point for your loop.
- // This is often represented as a variable "i".
- // You must assign this variable a value to start.
- // The CONDITION tells your code how long it should run.
- // Wherever your initial expression starts, this will be its destination.
- // The INCREMENT EXPRESSION tells your code how it should progress.
- // This is where the increment and decrement operators shine.
- // Example for loop:
- for(var i = 0; i < 10; i++) {return i;}
- // In that case, the code would begin at 0 and run until 9.
- // It would return each number between 0 and 9.
- // You could also go from 9 to 0 by simply swapping the initial expression
- // value and condition value, and having the increment expression DEcrement.
- // For loops are very useful for things like arrays, but do not
- // work on normal objects.
- // For example, you could loop through an array using bracket notation.
- // In the case of the previous loop, it would be like returning array[i].
- // This would not work with an object because objects have keys instead
- // of index numbers.
- // A FOR IN loop is similar to a for loop, but it can work with objects.
- // Its syntax much simpler than a simple for loop.
- // You start again by calling for
- // Then in parenthesis give a variable to represent the objects properties
- // Then say "in"
- // Then call the object you want to loop through
- // Syntax:
- // for(propertyVariable in object) {code to run}
- // Example for/in loop:
- var obj = {key1:1, key2:2, key3:3}
- for(i in obj) {return obj[i]} // returns all values of obj
- // A WHILE loop will loop WHILE a set condition is still true
- // This is similar to a for loop, but with a looser syntax.
- // You call WHILE,
- // Put a condition in the parenthesis
- // The condition should contain a variable that will change
- // Variables must be assigned before the loop runs
- // Then the code you put in curly braces will run
- // as long as the condition is true
- // For this loop you must put the increment INSIDE the curly braces.
- // Syntax:
- // while(condition) {code to run}
- // Example of while loop:
- var n = 10;
- while(n > 0) {n--; return n;} // returns 9 through 0</script></body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement