Instead of iterating through my loop with a classic iterator (when I say classic I mean iterating the loop using a fixed value with something like += 20).
I would like to iterate with a value which is increasing or decreasing as I progress through my loop iteration.
You need to make sure the loop reaches a terminal value if the accumulative increases is close to the accmulative decreases then the loop might not end for a very long time. For instance I ran this sketch
int idx = 1;
for(int v = 0; v >= -100 && v <= 100; v += 5 * cos(random(TWO_PI))){
println(idx++, v);
}
and it took 433954 iterations to stop but this varied a lot because of the random number. Changing it so we have v += 5 * cos(0.75 + random(TWO_PI)) reduces it a lot because positive values predominate.
My examples were intended to answer the original poster’s query by demonstrating that the increment in a for loop can vary. The examples should have been accompanied by a caveat.
In general, the header of a loop should be a simple control structure, with the bulk of the functionality contained in the body of the loop.
The second example was inspired by this code from the official Python web site:
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(1000)
As we can see, the work was performed, quite appropriately, in a while loop. But that algorithm presented an opportunity to demonstrate what could, but not necessarily what should, be done in a Java for loop header. In my example, all of the work except the output was performed in a for loop header.
So, with the caveats having been stated above, let’s carry that example to an even further extreme. Here is the loop again, with all the work contained in a header, and with no loop body at all:
void setup() {
for (int a = 0, b = 1, c;
a <= 144;
c = a, a = b, b = c + a, println(c));
}