|
| 1 | +--- |
| 2 | +chapter : "js10" |
| 3 | +section: "4" |
| 4 | +title: "Loops" |
| 5 | +--- |
| 6 | +Loops let you run a block of code a certain number of times. |
| 7 | + |
| 8 | +<div class="example" markdown="1"> |
| 9 | +Loops |
| 10 | + |
| 11 | + // logs 'try 0', 'try 1', ..., 'try 4' |
| 12 | + for (var i=0; i<5; i++) { |
| 13 | + console.log('try ' + i); |
| 14 | + } |
| 15 | +</div> |
| 16 | + |
| 17 | +<p class="note">Note that in Loops even though we use the keyword var before |
| 18 | +the variable name i, this does not "scope" the variable i to the loop block. |
| 19 | +We'll discuss scope in depth later in this chapter.</p> |
| 20 | + |
| 21 | +## The `for` loop |
| 22 | + |
| 23 | +A `for` loop is made up of four statements and has the following structure: |
| 24 | + |
| 25 | + for ([initialisation]; [conditional]; [iteration]) { |
| 26 | + [loopBody] |
| 27 | + } |
| 28 | + |
| 29 | +The _initialisation_ statement is executed only once, before the loop starts. It |
| 30 | +gives you an opportunity to prepare or declare any variables. |
| 31 | + |
| 32 | +The _conditional_ statement is executed before each iteration, and its return |
| 33 | +value decides whether or not the loop is to continue. If the conditional |
| 34 | +statement evaluates to a falsey value then the loop stops. |
| 35 | + |
| 36 | +The _iteration_ statement is executed at the end of each iteration and gives you |
| 37 | +an opportunity to change the state of important variables. Typically, this will |
| 38 | +involve incrementing or decrementing a counter and thus bringing the loop ever |
| 39 | +closer to its end. |
| 40 | + |
| 41 | +The _loopBody_ statement is what runs on every iteration. It can contain anything |
| 42 | +you want. You'll typically have multiple statements that need to be executed |
| 43 | +and so will wrap them in a block ( {...}). |
| 44 | + |
| 45 | +Here's a typical for loop: |
| 46 | + |
| 47 | +<div class="example" markdown="1"> |
| 48 | +A typical for loop |
| 49 | + |
| 50 | + for (var i = 0, limit = 100; i < limit; i++) { |
| 51 | + // This block will be executed 100 times |
| 52 | + console.log('Currently at ' + i); |
| 53 | + // Note: the last log will be "Currently at 99" |
| 54 | + } |
| 55 | +</div> |
| 56 | + |
| 57 | +## The `while` loop |
| 58 | + |
| 59 | +A while loop is similar to an if statement, except that its body will keep |
| 60 | +executing until the condition evaluates to false. |
| 61 | + |
| 62 | + while ([conditional]) { |
| 63 | + [loopBody] |
| 64 | + } |
| 65 | + |
| 66 | +Here's a typical while loop: |
| 67 | + |
| 68 | +<div class="example" markdown="1"> |
| 69 | +A typical `while` loop |
| 70 | + |
| 71 | + var i = 0; |
| 72 | + while (i < 100) { |
| 73 | + // This block will be executed 100 times |
| 74 | + console.log('Currently at ' + i); |
| 75 | + |
| 76 | + i++; // increment i |
| 77 | + } |
| 78 | +</div> |
| 79 | + |
| 80 | +You'll notice that we're having to increment the counter within the loop's |
| 81 | +body. It is possible to combine the conditional and incrementer, like so: |
| 82 | + |
| 83 | +<div class="example" markdown="1"> |
| 84 | +A while loop with a combined conditional and incrementer |
| 85 | + |
| 86 | + var i = -1; |
| 87 | + while (++i < 100) { |
| 88 | + // This block will be executed 100 times |
| 89 | + console.log('Currently at ' + i); |
| 90 | + } |
| 91 | +</div> |
| 92 | + |
| 93 | +Notice that we're starting at -1 and using the prefix incrementer (++i). |
| 94 | + |
| 95 | +## The `do-while` loop |
| 96 | + |
| 97 | +This is almost exactly the same as the while loop, except for the fact that the |
| 98 | +loop's body is executed at least once before the condition is tested. |
| 99 | + |
| 100 | + do { |
| 101 | + [loopBody] |
| 102 | + } while ([conditional]) |
| 103 | + |
| 104 | +Here's a do-while loop: |
| 105 | + |
| 106 | +<div class="example" markdown="1"> |
| 107 | +A do-while loop |
| 108 | + |
| 109 | + do { |
| 110 | + // Even though the condition evaluates to false |
| 111 | + // this loop's body will still execute once. |
| 112 | + |
| 113 | + alert('Hi there!'); |
| 114 | + |
| 115 | + } while (false); |
| 116 | +</div> |
| 117 | + |
| 118 | +These types of loops are quite rare since only few situations require a loop |
| 119 | +that blindly executes at least once. Regardless, it's good to be aware of it. |
| 120 | + |
| 121 | +## Breaking and continuing |
| 122 | + |
| 123 | +Usually, a loop's termination will result from the conditional statement not |
| 124 | +evaluating to true, but it is possible to stop a loop in its tracks from within |
| 125 | +the loop's body with the break statement. |
| 126 | + |
| 127 | +<div class="example" markdown="1"> |
| 128 | +Stopping a loop |
| 129 | + |
| 130 | + for (var i = 0; i < 10; i++) { |
| 131 | + if (something) { |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | +</div> |
| 136 | + |
| 137 | +You may also want to continue the loop without executing more of the loop's |
| 138 | +body. This is done using the continue statement. |
| 139 | + |
| 140 | +<div class="example" markdown="1"> |
| 141 | +Skipping to the next iteration of a loop |
| 142 | + |
| 143 | + for (var i = 0; i < 10; i++) { |
| 144 | + if (something) { |
| 145 | + continue; |
| 146 | + } |
| 147 | + |
| 148 | + // The following statement will only be executed |
| 149 | + // if the conditional 'something' has not been met |
| 150 | + console.log('I have been reached'); |
| 151 | + } |
| 152 | +</div> |
0 commit comments