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