Learn creative coding writing simple programs

23. Using the while loop

Introduction to the while loop. The structure is similar to an if statement, but there is an important difference. With an *if* statement, the code inside the curly braces will run 0 or 1 times (0 times if the condition is false, 1 time if the condition is true).
But with the while loop code can run many times, not only 0 or 1. It will keep running while the test condition is true. It is ideal to repeat some code many times.
In our example we use it to draw many circles. Just changing one value we can decide if we want 10 circles or 100. It's important that something inside the while loop will make the condition false, otherwise the loop runs forever and can make our computer slow. Always check that the loop will run only as many times as you expect, not forever!

Tags:

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); } ```