# Wrap around iteration

**URL:** <https://discourse.processing.org/t/wrap-around-iteration/12586>\
**Category:** Coding Questions\
**Created:** [July 8, 2019, 1:17pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586 "2019-07-08T13:17:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [July 8, 2019, 1:17pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/1 "2019-07-08T13:17:35Z")

</div>

Is there a way to get this effect of generating  
0, 1  
1, 2  
2, 3  
3, 0  
…  
with a single condition, I mean either without using if/else, or taking the if/else out of the for loop? And avoid hard coding ‘0’

This just seems janky to me

```auto
PVector[] points;
int nPoints;
float textSz;
void setup() {
  size(100, 100);
  textSz = 16;
  nPoints = 4;
  points = new PVector[nPoints];
}
void draw() {
  background(50);

  for (int i = 0; i < points.length; i++) {
    if (i+1 < points.length) {
      points[i] = new PVector(i, i+1);
    } else {
      points[i] = new PVector(i, 0);
    }
  }
  display();
}
void display() {
  for (int i = 0; i < points.length; i++) {
    textAlign(LEFT, TOP);
    textSize(textSz);
    String x = nf(points[i].x, 1, 0);
    String y = nf(points[i].y, 1, 0);
    String p = x + ", " + y;
    text(p, 0, i * textSz);
  }
}

```

---

<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:** [July 8, 2019, 1:48pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/2 "2019-07-08T13:48:31Z")

</div>

Hello,

Your code did not run as and needs some work.

This (only providing a hint) came to mind and worked:

```auto
  for (int i = 0; i < p.length; i++) 
    {
    // new Pvector
    // ?
    p[i].y = p[i%4].x+1;
    }

```

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/b/b8faceccff0450691df22613f906ade1842247a4.png)  
That modulo operator % comes in so handy!  
[https://processing.org/reference/modulo.html](https://processing.org/reference/modulo.html)

🙂

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [July 8, 2019, 3:53pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/3 "2019-07-08T15:53:38Z")

</div>

Sorry… what’s not working? It runs on the Processing iCompiler, which of course runs lots of things it shouldn’t, but maybe it’s that text() doesn’t like outputting a PVector?

I’ll update the code after I check it in the Processing IDE.

The output I get is

```auto
[0, 1, 0]
[1, 2, 0]
[2, 3, 0]
[3, 0, 0]

```

… which is what I’m trying for.

I don’t follow the example you provided, it should go back to 0, not 1

---

<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:** [July 8, 2019, 4:02pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/4 "2019-07-08T16:02:31Z")

</div>

> [@ddownn](#):
>
> Processing iCompiler

I am running on PC with Processing 3.5.3

I was incorrect with answer… back to drawing board.

🙂

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [July 8, 2019, 4:13pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/5 "2019-07-08T16:13:05Z")

</div>

I updated the code so it runs normally as-is.

---

<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:** [July 8, 2019, 4:16pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/6 "2019-07-08T16:16:26Z")

</div>

This worked:  
`p[i].y = (p[i].x+1)%4;`

![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/29534ba2d4f1abc4037fc4eb31a7c93bd0d94931.png)

I will leave the rest with you.

🙂

---

<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:** [July 8, 2019, 4:40pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/7 "2019-07-08T16:40:16Z")

</div>

```auto
PVector[] v = new PVector[4];

for(int i = 0; i < v.length; i++){
  v[i] = new PVector(i, v.length - 1 - i);
  println(v[i]);
}

```

Ignore this answer misread question 😊

---

<div class="post-metadata">

**Author:** ![InferNova](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/infernova/32/6009_2.png) [@InferNova](https://discourse.processing.org/u/InferNova)\
**Post date:** [July 8, 2019, 4:51pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/8 "2019-07-08T16:51:39Z")

</div>

Hey There!

Try this:

```auto
for(int i = 0 ; i < points.length ; i++){
	points[i] = new PVector( i , (i+1)%points.length );
}

```

This should work as we start from 0 , move up i+1 in the second index and wrap it back down depending on the array length by using modulo.

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [July 9, 2019, 12:04pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/9 "2019-07-09T12:04:40Z")

</div>

> [@quark](#):
>
> Ignore this answer

I’m sure this is an answer to a question I might have had in the near future, but now will not have to ask. Thank you in advance! 😀

What’s this called?

```auto
0 [0.0, 4.0, 0.0]
1 [1.0, 3.0, 0.0]
2 [2.0, 2.0, 0.0]
3 [3.0, 1.0, 0.0]
4 [4.0, 0.0, 0.0]

```

---

<div class="post-metadata">

**Author:** ![ddownn](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ddownn/32/2731_2.png) [@ddownn](https://discourse.processing.org/u/ddownn)\
**Post date:** [July 9, 2019, 12:05pm UTC](https://discourse.processing.org/t/wrap-around-iteration/12586/10 "2019-07-09T12:05:32Z")

</div>

Thank you, this is good.
