A loop saves you time in writing because you have to write a line (or 2 lines) only once instead of 12 or 1200 times.
So you tell processing to do those 1 or 2 or whatever lines again and again. How many times you can tell it.
Thus a program is easier to read.
Most common is the for-loop.
It says do the following lines within { and } again again, X times.
That's useful if you want to make something X times and you know how often beforehand.
e.g.
for (int i = 0; i < 40; i = i+1) {
line(30, i, 80, i);
}
Between { and } is the stuff that gets repeated.
To control how many times it gets repeated, we have the part between ( and ):
- we introduce a variable i.
- its start value is 0 (i=0);
- We run the loop while it's smaller 40 (i<40).
- With each time of the loop we increase it by 1 (i=i+1).
thus 40 lines are drawn, all between 30 and 80 (x-value) but increasing y-value marked by i.
Those values you can change. So play with it in your own program, make yourself comfy with it.
E.g. you could start at 60 or increase i by 5 or go up to 120.
Remember when you start at 60 you must also change the value of how long you run (make it i<100 instead of i < 40), otherwise processing will quit the loop immediately... (since 60 > 40).
See
http://www.processing.org/reference/for.html
Another loop is while
http://www.processing.org/reference/while.html