For loop structure for arrays

interesting! Other less-typical for loops:

for (int x=5; x<55; x+=5) { println(x); }  // step size
for (int x=100; x>=0; x--) { println(x); }  // countdown
for (int x=50; x!=49; x=(x+1)%100) { println(x); }  // wrap around
for (int x=0; x!=9; x+=int(random(-3,3))) { println(x); }  // random walk

These may be considered bad style, however. A countdown or +2 is easy enough, but once a for loop starts to seem “fancy” it is getting hard to read, and that may be a good time to ditch it for incrementing by +1 and a count, and move the scaling / shifting inside – or turn it into a while loop to focus on a complex stopping condition.

4 Likes