Agenda

Loops

In class we played with loops within the following P5 sketch:

https://editor.p5js.org/des8963/sketches/4edG5m6tk

We covered FOR LOOPS in the class, but P5 & Javascript also has WHILE LOOPS. Here is an example of both. (Admittedly it is uncommon for me to see people using while loops, but there are some reasons why this is sometimes preferred)

// Basic syntax of a while loop statement:

while (expression) {
  // do something
}

// Example of a while loop:

let counter=0;
while (counter < 25) {
  console.log("Our count is ",counter);
  counter++;  ## Warning!!! make sure your loop has a way to end
}

// Basic syntax of a for loop statement:

for (initialization; conditionExpression; update) {
    // do something
}

# alternative
for (let counter=0; counter <25; counter++) {
  console.log("Our count is ",counter);
}

Example 2: Nested Loop:

Covered Nested Loops [example]