Up until now we have had only one way to repeat code – by placing it in the draw() where it is called again and again.
For example, if we wanted to make many circles starting at x = 0 and covering the canvas horizontally, we could write
We have control over the initial value of x, and could extend our control by limiting how long we increment x with some conditional statement. For example, we could start at x = 50 and stop at x = 200 with the following code.
This works, but it relies on the looping nature of the draw() function.
While and for loops allow us to repeat code without relying on the draw() function.
The while loop has the same syntax as an if conditional statement.
Means something very similar to
They both mean, "if a condition is met, namely that x < 250, execute the block of code in curly brackets. The difference is that the while statement does not rely on the looping function to repeat the code.
How would we draw a set of circles using the while loop?
Since the while loop does not rely on the looping feature of the draw() function, we could place the code in setup() and achieve the same result.
Example 4.1.1: Drawing 4 circles with a while loop