# Problem making a wrap around loop with modulo

**URL:** <https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851>\
**Category:** Coding Questions\
**Created:** [February 16, 2020, 5:40am UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851 "2020-02-16T05:40:10Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 16, 2020, 5:40am UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/1 "2020-02-16T05:40:10Z")

</div>

I’m trying to make a wrap around loop with modulus. But it’s not working. Keep getting error “expecting {, found [”.  
Showing function for loop appears in.  
If anyone can help, many thanks!

```auto
color[]getThreeColorLerp() { 

  color start = color (245, 17, 48);
  color midpoint = color (250, 200, 63);
  color end = color(12, 187, 229);

  int arraySize = 20;
  float step = 1.0f / arraySize;

  IntList colorGroup_1 = new IntList ();

  // wrap around loop with modulo not working
  for (int k = 0; k < arraySize*5; k++) {

    [k] = k % arraySize;
  }

  println(k);

  float m = map (step, 0.0, 1.0, 0.0, 3.3);

  if (k * m <= 1.0) {

    colorGroup_1.set (k, lerpColor(start, midpoint, k*m));
    println(k*m);
  } else if (k*m > 1.0 && k*m <= 2.0) {

    colorGroup_1.set (k, lerpColor(midpoint, end, (k*m)-1.1));
    println(k*m);
  } else {

    colorGroup_1.set (k, lerpColor(end, start, (k*m)-2.2));
    println(k*m);
  }
  return colorGroup_1.array();
}

```

---

<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:** [February 16, 2020, 8:53am UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/2 "2020-02-16T08:53:11Z")

</div>

> [@debxyz](#):
>
> ```
> [k] =
> 
> ```

That won’t work

You need

- an array to use [k]: list[k]

- or you say k = …

- or using Intlist you want something like `colorGroup1.get( k % arraySize ) ;`

See reference

Chrisir

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 16, 2020, 1:03pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/3 "2020-02-16T13:03:38Z")

</div>

I’m realizing I’m in over my head with the wrap around loop. So am setting this project aside for now… I’ll come back to it when I have a broader foundation.  
But I do appreciate your suggestions above.

---

<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:** [February 16, 2020, 2:56pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/4 "2020-02-16T14:56:46Z")

</div>

Hello,

Consider:

- Writing small and simple functional blocks first to understand every element of code.
- Using console prints, writing text to canvas, coloring, animating, etc. to visualize and understand code.

I saw “wrap around loop using modulo” and wrote this simple example:

```auto
// Simple Wrap Around Loop Using Modulo
// v1.0.0
// GLV 2012-02-16

int [] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //16 elements 
int offset;

void setup() 
	{
  offset = 5;
  for (int i = 0; i< data.length; i++)
    { 
    int w = (i+offset)%data.length;
    println(i, data[i] , data[w]);
    }
  }

```

I then modified (write text to canvas, added color, animated, etc. ):

> **Wrap around loop (enhanced)**
>
> ```auto
> // Simple Wrap Around Loop Using Modulo
> // v2.0.0
> // GLV 2012-02-16
> 
> int [] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; //16 elements 
> int offset;
> 
> void setup() 
> {
> size(700, 100);
> textSize(24);
> }
> 
> void draw() 
> {
> background(0);
>   
> if (frameCount%30 == 0) offset++; // increments offset every 30 frames
> if (offset > data.length) offset = 0;
>   
> for (int i = 0; i< data.length; i++)
> { 
> int w = (i+offset)%data.length;
>     
> // no wrapping
> if(i >= offset) fill(255, 0, 0); else fill(0, 255, 0);    
> text(data[i], i*40 + 40, height/2);    
>     
> // wrapping
> if(w >= offset) fill(255, 0, 0); else fill(0, 255, 0);
> text(data[w], i*40 + 40, height/2+30); // wrapping
>     
> println(i, data[i] , data[w]);
> }
> }
> 
> ```

’ 🙂 ’

---

<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:** [February 16, 2020, 3:16pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/5 "2020-02-16T15:16:58Z")

</div>

modulo is simple, it takes the remainder of a division

```auto
for (int i=0; i < 16; i++) 
  println (i+" -> " + i%5); //or k=i%5;

```

gives you

```auto

0 -> 0
1 -> 1
2 -> 2
3 -> 3
4 -> 4
5 -> 0
6 -> 1
7 -> 2
8 -> 3
9 -> 4
10 -> 0
11 -> 1
12 -> 2
13 -> 3
14 -> 4
15 -> 0

```

So you can use it when your array has only 5 elements (index 2nd column), but your for loop is longer than 5 (first column above).

You can also write

```auto
k++;
if(k>4) i=0;

```

which I personally find much better to read

Chrisir

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 16, 2020, 5:51pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/6 "2020-02-16T17:51:41Z")

</div>

> [@Chrisir](#):
>
> So you can use it when your array has only 5 elements (index 2nd column), but your for loop is longer than 5 (first column above).

Understood.

> [@Chrisir](#):
>
> You can also write
> 
> ```auto
> k++;
> if(k>4) i=0;
> 
> ```

Ah ha! This way looks more straightforward. I will try this!  
Thank you!!

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 16, 2020, 5:56pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/7 "2020-02-16T17:56:35Z")

</div>

> [@glv](#):
>
> - Writing small and simple functional blocks first to understand every element of code.
> - Using console prints, text to canvas, color, animate etc. to visualize and understand code.

Thank you, that’s good advice. I will start doing this more going forward!!

---

<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:** [February 16, 2020, 6:33pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/8 "2020-02-16T18:33:59Z")

</div>

> [@debxyz](#):
>
> wrap around loop with modulo not working  
> for (int k = 0; k \< arraySize\*5; k++) {  
> [k] = k % arraySize;

You also had tried to change the variable of your for loop

This is not recommended and you should know what you are doing if you do it

Instead take a new variable (k2 or so) like in my example above

---

<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:** [February 18, 2020, 8:25pm UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/9 "2020-02-18T20:25:18Z")

</div>

here is an example using modulo

please note that modulo is **not** used here to _make_ an array but to _read_ the content of an array (use it)

more precise, we have more slots on the screen to fill with a color than the color array has elements.

To avoid that we read over the end of the color array, we use % modulo, so that after the end of the array we restart reading the color array from the beginning (in setup() it’s this line: `shuffledInventory[i % shuffledInventory.length]` ).

Alternative to modulo would be (as I said) `... shuffledInventory[k] ...` and then

```auto
k++; 
if(k>shuffledInventory.length) 
    k=0;

```

Chrisir

```auto

// global array 
Cell [] cells = new Cell [39];

void setup() {
  size (900, 400);

  // NOT shuffled at the moment 
  color[] shuffledInventory = getColorListShuffled(); 
  println(shuffledInventory.length);

  int x = 20, y = x;
  int w = (width-40)/cells.length, h = 360;

  for (int i = 0; i < cells.length; i++) {
    cells[i] = new Cell(x+i*w, y, 
      w, h, 
      shuffledInventory[i % shuffledInventory.length] );
  }//for
}//func

void draw() {
  background (51);

  for (int i = 0; i < cells.length; i++) {
    cells[i].display();
  }//for
}//func 

//-----------------------------------------------------------------------------------------
// Tools 

color[] getColorListShuffled() {

  // for the command lerpColor: 
  color from = color(random(255), random(255), random(255)); 
  color to = color(random(255), random(255), random(255));
  float step = 1.0f / (10-1); // **must use a float number to return a float number**

  IntList inventory = new IntList(); // stores list of colors

  // for loop to generate an IntList of random colors
  for (int k = 0; k < 10; k++) {
    inventory.set(k, lerpColor (from, to, k*step));
  }
  // inventory.shuffle(); // NOT randomly shuffles the list inventory

  // directs the inventory 
  // as an array to 
  return inventory.array();
}

///////////////////////////////////////////////////////////////////////////////////////////

class Cell {
  float x, y, w, h;
  color clr;

  Cell ( 
    float tempX, float tempY, 
    float tempW, float tempH, color tempClr) {

    x = tempX;
    y = tempY;
    w = tempW;
    h = tempH;
    clr = tempClr;
  }
  void display() {

    stroke(255); 
    fill(clr);
    rect(x, y, w, h);
  }
  //
}//class
//

```

---

<div class="post-metadata">

**Author:** ![debxyz](https://avatars.discourse-cdn.com/v4/letter/d/58956e/32.png) [@debxyz](https://discourse.processing.org/u/debxyz)\
**Post date:** [February 19, 2020, 2:09am UTC](https://discourse.processing.org/t/problem-making-a-wrap-around-loop-with-modulo/17851/10 "2020-02-19T02:09:59Z")

</div>

Ah! That makes sense! I had overthought it and was way off track…  
Thank you!I  
🙂
