Anatomy of the For-Loop • tim rodenbröker creative coding

Anatomy of the For-Loop

Courses / Mastering the For-Loop / Anatomy of the For-Loop

For grid systems, understanding the for-loop is of elementary importance. That’s why we’ll take a close look at this one.

Processing

void setup() {
  size(900, 900);
}

void draw() {
  background(0);

  for (int i = 0; i < 10; i++) {
    println(i); // returns 0,1,2,3,4,5,6,7,8,9
  }
}

P5.js

function setup() {
  createCanvas(900, 900);
}

function draw() {
  background(0);

  for (let i = 0; i < 10; i++) {
    print(i); // returns 0,1,2,3,4,5,6,7,8,9
  }
}

The principle is not very easy to understand and to be honest, I really had problems to understand it in depth for a relatively long time. But it helps a lot to have a close look at all elements of the for-loop, because many answers to future questions arise from it.


Here a variable with the name i is declared, which gets the value 0. i is here really only the name of the variable. You could just as well call it something else. However, the name i has become established over the years.


This is a condition as we know it from the conditional statements: In this case the loop is only executed as long as i is less than 100.


This somewhat perhaps confusing passage says nothing other than “add 1 to i“.


Example

//Processing
for (int i = 0; i < 100; i++){
   // this code executes 100 times
}
//P5.js
for (let i = 0; i < 100; i++){
   // this code executes 100 times
}

Language Comparison

Processingp5.js
forfor
Published by Tim on Wednesday December 30, 2020

Last modified on January 18th, 2023 at 12:35

  1. Anatomy of the For-Loop
  2. Random Distribution
  3. Linear Distribution
  4. Circular Distribution
  5. Circular Distribution (Examples)
  6. Two-dimensional distribution
  7. Two-dimensional distribution (Examples)
  8. Truchet Patterns
  9. Centering a Grid System
  10. Wrapping Up