Intro to iterations

The Break Statement

  • We use it to terminate a loop/switch ### Output 0
    1
    2
    3
    4
    5
    When i = 6, the break statement is run, and it ends the loop early ## Continue Statement used to restart a while or for loop
  • when used, ends the current iteration, and continues the next iteration ### Output 0
    1
    2
    3
    4
    5
    7
    8
    9
    Notice how it skips 6 and continues the loop ## For each loops We did this in the previous lesson loop through an array There is a different way to do this These blocks of code both have the same output ### Output red
    blue
    yellow
    green
    black
    ```python %%js for (let i = 0; i < 10; i++) { if (i == 6) { break; } console.log(i); } ``` ```python %%js const colors = ['red','blue','yellow','green','black']; //color = i for (let color of colors) { //checks each element in the list console.log(color); //prints each color } ``` ```python %%js const colors = ['red','blue','yellow','green','black']; for (let i = 0; i < colors.length; i++){ console.log(colors[i]); } ```