Break and Continue:

  • Break:
  • Iterating through a loop is useful, but what if we need to stop iterating at a certain point? To do this, we use the break; statement. Note in the code block below where the statement is in relation to what we want the code to do. Our alert etc. needs to be outside of the break statement:

    1. For loop with break:
    2. Let's return to our Dogs d'Amour albums [array]:


      let dogsAlbums = ["(Un)authorised Bootleg Album", "Dynamite Jet Saloon", "Errol Flynn", "Straight", "More Unchartered Heights Of Disgrace"];


      We want to break the code block at index #3 - in other words, we want indices 0, 1 and 2, but not index #3:

      Note that we are looking to stop at an index #, not at a specific enry in the [array]. In other words, we want to know what the entry is at the given index #. If we replace the i === 3 with the name of one of the albums, the loop will not break, but will continue until the .length is at its end:


    3. For... in loop with break:
    4. Again, let's break at index #3:

      Since the second and third conditions (i<[array].length; i++) are implied, we do not need these in the loop:


      Note that i === 3 will not work in this situation, as the number 3 would be treated as a "string" if it were. We are not looking for an is identical to.


      Again, note that with the for... in loop, the values of the [array] could be anything. We are looking for the index # (that is, does it entry #3 exist in the [array]?) rather than its value.


    5. For... of loop with break:
    6. Again, let's break at index #3:

      Again, the second and third conditions of the loop are implied. But with the for... of loop, we can name i whatever we want. I have named it "album".

      With the for... of loop, we are looking for the "string" value at a certain index #, not the index # itself, and because of this we need to look for an is identical to (===):


    7. While loop with break:
    8. Note that with while loops, we need to declare the variable i. For loops self declare their starting points, whereas while loops are conditional loops that function until a condition is either met or not (depending on the code to be executed).


      Again, let's break at index #3:

      We are looking for if index #3 exists. Not its value. Thus we do not need an is identical to (===):


    9. Do... while loop with break:
    10. Note that, again, with do... while loops, we need to declare the variable i. We also need to tell the code what to alert/log/print BEFORE we run the loop (otherwise, we get an infinite loop). Thus, here, we tell the code to:

      Alert the result

      Increment

      before we tell the loop where to break.


      Again, let's break at index #3:

      Again, we are looking for if index #3 exists. Not its value. Thus we do not need an is identical to (===):


  • Continue:
  • Break loops are great if we want to stop a block of code from looping. But what if we want to skip certain item in an [array] and then continue? This is where the continue keyword comes in useful: we can skip even numbers, odd numbers, numbers that are divisible by x... and of course, we can skip "string" values in [arrays] using this keyword.

    1. For loop using continue:
    2. We can see that the code is exactly the same, save for the keyword continue.

      numArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];


      Let's try the same with our Dogs d'Amour albums [array]:


    3. For... in loop with continue:

    4. For... of loop with continue:

    5. While loop with continue:
    6. Note how we have had to slightly change the ordering of the code for the loop to work with continue. The same is true for the do... while loop:


    7. Do... while loop with continue: