# Why backwards-for-loops don't work?

**URL:** https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672
**Category:** Libraries
**Created:** [September 4, 2020, 7:16am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672 "2020-09-04T07:16:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [September 4, 2020, 7:16am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/1 "2020-09-04T07:16:24Z")

</div>

Hello,

How come a for-loop can’t run backwards?

I sometimes need to run backwards through arrays and wanted something shorter than a while loop and reverse() doesnt seem to work for higher-D arrays e.g. reverse(array)

Have tried in a few sketches and never works - the code just gets ignored completely - doesn’t even error.

Have tried googling and found advice telling people to reverse arrays but never the underlying reason why?

eg using the fft example code

```auto
import processing.sound.*;

FFT fft;
AudioIn in;
int bands = 128;
float[] spectrum = new float[bands];

void setup() {
  size(512, 360);
  background(255);
    
  // Create an Input stream which is routed into the Amplitude analyzer
  fft = new FFT(this, bands);
  in = new AudioIn(this, 0);
    // start the Audio Input
  in.start();
    // patch the AudioIn
  fft.input(in);
}      

void draw() { 
  background(255);
  fft.analyze(spectrum);

//backwards for loop - doesnt work
for(int i = bands-1; i < 0; i--){
line( i, height, i, height - spectrum[i]*height*5 );
} 
  
//more manual backwards loop - does work
//int j = bands-1;
//while(j>0){
//line( j, height, j, height - spectrum[j]*height*5 );
//j--;
//}

}

```

Seems to just be a thing but was curious as to the reasons why

Thank you kindly

---

<div class="post-metadata">

### Author: ![SomeOne](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/someone/32/8639_2.png) [@SomeOne](https://discourse.processing.org/u/SomeOne)
#### Post date: [September 4, 2020, 7:40am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/2 "2020-09-04T07:40:11Z")

</div>

> [@Tinkerer](#):
>
> `for(int i = bands-1; i < 0; i--){`

I think it should be

> [@Tinkerer](#):
>
> `for(int i = bands-1; i > 0; i--){`

Original version never starts because i (128-1) is not smaller than zero.

---

<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: [September 4, 2020, 7:58am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/3 "2020-09-04T07:58:32Z")

</div>

If you want to include element[0] then it should be

```auto
for(int i = bands-1; i >= 0; i--){

```

---

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [September 4, 2020, 8:40am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/4 "2020-09-04T08:40:25Z")

</div>

many thanks for that - works now 🙂

I was thinking the conditional was an until statement when it should a while statement  
(even though looking at a forwards loop example should have clued me in - just didnt make the connect!)

---

<div class="post-metadata">

### Author: ![Tinkerer](https://avatars.discourse-cdn.com/v4/letter/t/f14d63/32.png) [@Tinkerer](https://discourse.processing.org/u/Tinkerer)
#### Post date: [September 4, 2020, 8:41am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/5 "2020-09-04T08:41:13Z")

</div>

Thanks muchly - I’ll give it a go 🙂

---

<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: [September 4, 2020, 10:53am UTC](https://discourse.processing.org/t/why-backwards-for-loops-dont-work/23672/6 "2020-09-04T10:53:29Z")

</div>

> [@Tinkerer](#):
>
> I was thinking the conditional was an until statement when it should a while statement

Yep, and funnily enough it defaults to true, so if you want `while (true)` you can also write `for (;;)`, should you wish to save a few characters, as well as confuse a few characters! 😄
