Why doesn't FOR-EACH give every output a different VALUE from ARRAY?

I made an array that I output in a loop. But every item I assign the array it keep giving the same value while I’ve put 4 values into the array. I’ve tried everything but it keeps giving the same value (330). I have also made use of a for loop but it gives the same results. Thank you.

  int[] sx = {330,390,335,345};
  
  for (int i : sx){
    copy(video, i, 170, 40, 40, 0, 0, video.width/4, video.height/4);
    
    copy(video, i, 170, 40, 40, 150, 150, video.width/4, video.height/4); 
    
    copy(video, i, 205, 40, 40, 300, 300, video.width/4, video.height/4);
    
    copy(video, i, 230, 60, 40, 400, 150, video.width/4, video.height/4);
    print(i);
  }

image
When I used print to see the output of the value it always starts with 330, is that the problem?

It is not clear what you are trying to do and your explanation as to the problem doesn’t help.

If you try

int[] sx = {330, 390, 335, 345};

for (int i : sx) {
  println(i);
}

you get the following output

330
390
335
345

which is exactly what you would expect.

So in your code you have 4 copy statements which are executed for each integer in the array. If we expand your code to remove the loop and shaow what is actually being executed we get

int i;

i = 330;
copy(video, i, 170, 40, 40, 0, 0, video.width/4, video.height/4);
copy(video, i, 170, 40, 40, 150, 150, video.width/4, video.height/4); 
copy(video, i, 205, 40, 40, 300, 300, video.width/4, video.height/4);
copy(video, i, 230, 60, 40, 400, 150, video.width/4, video.height/4);
print(i);

i = 390;
copy(video, i, 170, 40, 40, 0, 0, video.width/4, video.height/4);
copy(video, i, 170, 40, 40, 150, 150, video.width/4, video.height/4); 
copy(video, i, 205, 40, 40, 300, 300, video.width/4, video.height/4);
copy(video, i, 230, 60, 40, 400, 150, video.width/4, video.height/4);
print(i);

i = 335;
copy(video, i, 170, 40, 40, 0, 0, video.width/4, video.height/4);
copy(video, i, 170, 40, 40, 150, 150, video.width/4, video.height/4); 
copy(video, i, 205, 40, 40, 300, 300, video.width/4, video.height/4);
copy(video, i, 230, 60, 40, 400, 150, video.width/4, video.height/4);
print(i);

i = 345;
copy(video, i, 170, 40, 40, 0, 0, video.width/4, video.height/4);
copy(video, i, 170, 40, 40, 150, 150, video.width/4, video.height/4); 
copy(video, i, 205, 40, 40, 300, 300, video.width/4, video.height/4);
copy(video, i, 230, 60, 40, 400, 150, video.width/4, video.height/4);
print(i);

the copy function is executed 16 times.

3 Likes

One remark

Processing doesn’t update the screen throughout

So when you expect to see the changes that happen inside the for loop you won’t see it.

Is this your issue?

1 Like