Learn creative coding writing simple programs

34. Create a grid of objects with nested while loops

We have seen in previous episodes how to create many objects at once using a while loop. This time we use a while loop inside a while loop to create a grid of objects. The result is a collection of objects (rectangles or circles in our example) distributed in the screen in a pattern similar to the white and black rectangles in a chess game.
Using a loop inside a loop is something very common in programming, and we will see more examples in coming episodes.

Tags: while, nested, loops, grid

Code editor

You can make changes to the code below. Then

Questions and comments

Try to stay close to the topic of this episode. Use the Processing forums for help with unrelated Processing projects (or hire me for help ;-)

To indicate that a word in your comment is code, use the `backtick`. Example
Do `float` and `int` smell similar?
To highlight code blocks, surround it with ``` code-fences ``` like this:
``` void setup() { size(600, 600); } ```