# For loop structure for arrays

**URL:** https://discourse.processing.org/t/for-loop-structure-for-arrays/19250
**Category:** Beginners
**Created:** [March 31, 2020, 2:50pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250 "2020-03-31T14:50:57Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)
#### Post date: [March 31, 2020, 2:50pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/1 "2020-03-31T14:50:57Z")

</div>

Hello,

I’m trying to wrap my head around the [for loop structure for arrays](https://processing.org/reference/for.html). 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.

```auto
float[] cats = new float[10];

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

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

```

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 31, 2020, 3:14pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/2 "2020-03-31T15:14:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)
#### Post date: [March 31, 2020, 3:18pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/3 "2020-03-31T15:18:25Z")

</div>

Hi Chrisir,

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

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [March 31, 2020, 3:21pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/4 "2020-03-31T15:21:12Z")

</div>

I don’t know

Apologies

---

<div class="post-metadata">

### Author: ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)
#### Post date: [March 31, 2020, 3:51pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/5 "2020-03-31T15:51:30Z")

</div>

> [@SheCurvesMobius](#):
>
> Is there a way to assign values to the elements in the array using the array for loop structure: for(int i : arrayName)?

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.

---

<div class="post-metadata">

### Author: ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)
#### Post date: [March 31, 2020, 3:52pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/6 "2020-03-31T15:52:31Z")

</div>

Ok, super–thanks quark 🙂

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [March 31, 2020, 4:04pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/7 "2020-03-31T16:04:02Z")

</div>

- 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.

---

<div class="post-metadata">

### Author: ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)
#### Post date: [March 31, 2020, 6:00pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/8 "2020-03-31T18:00:33Z")

</div>

Hi GoToLoop.

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

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 1, 2020, 1:05am UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/9 "2020-04-01T01:05:39Z")

</div>

Hello,

The Processing [reference](https://processing.org/reference/for.html) calls this form:

> A second type of _for_ structure.

[https://docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html](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](https://processing.org/tutorials/arrays/) A paragraph near bottom.  
[Array / Reference / Processing.org](https://processing.org/reference/Array.html) A comment in code.

---

<div class="post-metadata">

### Author: ![noel](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/noel/32/213_2.png) [@noel](https://discourse.processing.org/u/noel)
#### Post date: [April 2, 2020, 2:18am UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/10 "2020-04-02T02:18:25Z")

</div>

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.

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

```

---

<div class="post-metadata">

### Author: ![SheCurvesMobius](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/shecurvesmobius/32/7054_2.png) [@SheCurvesMobius](https://discourse.processing.org/u/SheCurvesMobius)
#### Post date: [April 2, 2020, 6:39pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/11 "2020-04-02T18:39:12Z")

</div>

Thanks glv and noel!

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [April 13, 2020, 7:00am UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/12 "2020-04-13T07:00:59Z")

</div>

> [@noel](#):
>
> I recently found out that the following is possible as well.

interesting! Other less-typical for loops:

```auto
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.

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [April 14, 2020, 10:00pm UTC](https://discourse.processing.org/t/for-loop-structure-for-arrays/19250/13 "2020-04-14T22:00:42Z")

</div>

> [@jeremydouglass](#):
>
> once a for loop starts to seem “fancy” it is getting hard to read

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

> [@jeremydouglass](#):
>
> or turn it into a while loop to focus on a complex stopping condition.

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