P5.js Course

Hi everyone,

I just started the p5.js course on codecademy two days ago. I’m actually kinda struggling with the first steps, which are the for loops. Could someone explain me the exact structure of a for loop drawing a pattern of circles for example? I find that kinda complicated with the codecademy resources.

Thanks, have a great day!

I drew a simple example. I find working on paper or a whiteboard helps me work out new concepts better (personally).

It’s important to note the structure for the for-loop:

for (starting-boundary; continue-condition; increment-step)
{
   //code to be looped
}

So for every for-loop we start with an initial boundary (often called i). The increment-step will decide how i is changed. Sometimes it is increased, but sometimes you will see it decrease, or jump by 2’s.

In every iteration of the loop (iteration meaning cycle around the loop): if the continue-condition is true, then the loop code executes in its entirety once more, and we change i by the increment step.

Notice in the provided example, for (let i = 0; i < 5; i++) the loop code is executed 5 times (in this case printing 5 rows).
You can easily change the parameters of the loop to make it operate as much or as little as you need.

3 Likes

thank you! Indeed I like to write things down too, now I do understand!