This reference is for Processing 2.0+. If you have a previous version, use the reference included with your software. If you see any errors or have suggestions, please let us know. If you prefer a more technical reference, visit the Processing Javadoc.

Name

for

Examples
example pic
for (int i = 0; i < 40; i = i+1) {
  line(30, i, 80, i);
}
example pic
for (int i = 0; i < 80; i = i+5) {
  line(30, i, 80, i);
}
example pic
for (int i = 40; i < 80; i = i+5) {
  line(30, i, 80, i);
}
example pic
// Nested for() loops can be used to
// generate two-dimensional patterns
for (int i = 30; i < 80; i = i+5) {
  for (int j = 0; j < 80; j = j+5) {
    point(i, j);
  }
}
Description Controls a sequence of repetitions. A for structure has three parts: init, test, and update. Each part must be separated by a semicolon (;). The loop continues until the test evaluates to false. When a for structure is executed, the following sequence of events occurs:

1. The init statement is executed.
2. The test is evaluated to be true or false.
3. If the test is true, jump to step 4. If the test is false, jump to step 6.
4. Execute the statements within the block.
5. Execute the update statement and jump to step 2.
6. Exit the loop.

In the first example above, the for loop is executed 40 times. In the init statement, the value i is created and set to zero. i is less than 40, so the test evaluates as true. At the end of each loop, i is incremented by one. On the 41st execution, the test is evaluated as false, because i is then equal to 40, so i < 40 is no longer true. Thus, the loop exits.
Syntax
for (init; test; update) { 
  statements
} 
Parameters
init statement executed once when beginning loop
test if the test evaluates to true, the statements execute
update executes at the end of each iteration
statements collection of statements executed each time through the loop
Relatedwhile
Updated on December 7, 2012 08:53:47am PST

Creative Commons License