# Begginer here, trying to reduce code with a for loop iteration

**URL:** <https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763>\
**Category:** Coding Questions\
**Created:** [September 14, 2022, 10:28pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763 "2022-09-14T22:28:42Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 14, 2022, 10:28pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/1 "2022-09-14T22:28:42Z")

</div>

> please format code with \</\> button \* [homework policy](https://discourse.processing.org/faq/#homework) \* [asking questions](https://discourse.processing.org/t/2147)

I used for loop for simple things before… but now I have to do some addition in every increment.  
My brain froze… I am drowning in code… Is there an easier way to write this?

Thanks

```auto
  TimeUp (Bl_1[0], 0, FPM_1[0], State_1[0]); // duration, start point
  TimeUp (Bl_1[1], Bl_1[0]+0, FPM_1[1], State_1[1]);
  TimeUp (Bl_1[2], Bl_1[0]+Bl_1[1], FPM_1[2], State_1[2]);
  TimeUp (Bl_1[3], Bl_1[0]+Bl_1[1]+Bl_1[2], FPM_1[3], State_1[3]);
  TimeUp (Bl_1[4], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3], FPM_1[4], State_1[4]);
  TimeUp (Bl_1[5], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4], FPM_1[5], State_1[5]);
  TimeUp (Bl_1[6], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4]+Bl_1[5], FPM_1[6], State_1[6]);
  TimeUp (Bl_1[7], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4]+Bl_1[5]+Bl_1[6], FPM_1[7], State_1[7]);
  TimeUp (Bl_1[8], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4]+Bl_1[5]+Bl_1[6]+Bl_1[7], FPM_1[8], State_1[8]);
  TimeUp (Bl_1[9], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4]+Bl_1[5]+Bl_1[6]+Bl_1[7]+Bl_1[8], FPM_1[9], State_1[9]);
  TimeUp (Bl_1[10], Bl_1[0]+Bl_1[1]+Bl_1[2]+Bl_1[3]+Bl_1[4]+Bl_1[5]+Bl_1[6]+Bl_1[7]+Bl_1[8]+Bl_1[9], FPM_1[10], State_1[10]);

```

---

<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:** [September 14, 2022, 10:33pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/2 "2022-09-14T22:33:38Z")

</div>

Yes, there is.

Chrisir

---

<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:** [September 14, 2022, 10:40pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/3 "2022-09-14T22:40:03Z")

</div>

For loop around the thing, one line

Replace 2nd parameter with a function that receives a number and returns the sum of the array elements up to that number using a for loop

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 14, 2022, 10:56pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/4 "2022-09-14T22:56:40Z")

</div>

Thanks a lot buddy, you helped before. I didnt think of the function.  
Mitch

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [September 15, 2022, 12:24pm UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/5 "2022-09-15T12:24:20Z")

</div>

Hi @laptophead,

If you don’t want to use a special function you can use such a logical approach

Cheers  
— mnse

```auto
// assuming numerical values
float v = 0;
for (int i=0;i<iterations;i++) {
  TimeUp (Bl_1[i], v, FPM_1[i], State_1[i]);
  v += Bl_1[i];
}

```

---

<div class="post-metadata">

**Author:** ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)\
**Post date:** [September 16, 2022, 3:12am UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/6 "2022-09-16T03:12:33Z")

</div>

MNSE  
That actually worked. Thanks a lot, I love the simplicity.

so v += Bl\_1[i]; means to add all the Bls from zero to i?  
Mitch

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [September 16, 2022, 5:49am UTC](https://discourse.processing.org/t/begginer-here-trying-to-reduce-code-with-a-for-loop-iteration/38763/7 "2022-09-16T05:49:00Z")

</div>

Hi @laptophead,

it does exactly what your code do …

```auto
1st iteration v=0
2nd iteration v=0+Bl_1[0]
3rd iteration v=0+Bl_1[0]+Bl_1[1]
...
Nth iteration v=0+Bl_1[0]+Bl_1[1]+ ... + Bl_1[N-1]

```

> [@laptophead](#):
>
> so v += Bl\_1[i]; means to add all the Bls from zero to i?

So on each iteration v is changed to the above to save it for the next iteration.

Cheers  
— mnse
