For loop structure for arrays

Hello,

I’m trying to wrap my head around the for loop structure for arrays. Is there a way to assign values to the elements in the array using the array for loop structure: for(int i : arrayName)?

I’ve done this but it’s not working.

float[] cats = new float[10];

void setup() {
  for (float k : cats) {
    k = random(50, 100);
  }

  for (float i : cats) {
    println(i);
  }
}

for(int i=0; i<cats.length; i++)
cats[i]=i+9;

Hi Chrisir,

Thanks for your response. :slight_smile:
I’m aware of that method. I’m wondering if there’s a way to do it using the
for(int i : arrayName){
method?

1 Like

I don’t know

Apologies

If the data type is a primitive (int, float, boolean etc) or if the data type is an immutable class (Integer, Float, Boolean etc) then the answer is no.

1 Like

Ok, super–thanks quark :slight_smile:

  • In order to access an array, like any other object, we need its reference.
  • In your case, cats is the field variable that holds the array reference.
  • The iterable variable k holds a copy of an item of the cats[] array each iteration.
  • Mutating k’s copy doesn’t change the original value inside the cats[] array.
  • So if we need to mutate the contents of cats[], we need a vanilla for ( ; ; ) style loop.
  • Use the for ( : ) style when you just need to read the contents rather than change them.
4 Likes

Hi GoToLoop.

Thanks for this in-depth explanation! It’s super helpful :slight_smile:

1 Like

Hello,

The Processing reference calls this form:

A second type of for structure.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html states:

This form is sometimes referred to as the enhanced for statement, and can be used to make your loops more compact and easy to read.

:)

Update:

There is more discussion about the enhanced for loop in the Processing references here:
Arrays / Processing.org A paragraph near bottom.
Array / Reference / Processing.org A comment in code.

3 Likes

Talking about for loops; also as a self-teaching beginner and maybe of interest to others, I recently found out that the following is possible as well.

int x = 2; 
for (long y = 0, z = 4; x < 10 && y < 10; x++, y++) { 
   println(x+" "+y);
 }
3 Likes

Thanks glv and noel!

1 Like

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

It may also become harder to optimize, so perform less well.

Or the other less typical for loop for (;;)

1 Like