# How to non linear iterator

**URL:** <https://discourse.processing.org/t/how-to-non-linear-iterator/31235>\
**Category:** Coding Questions\
**Created:** [July 13, 2021, 4:04pm UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235 "2021-07-13T16:04:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![goldorak](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@goldorak](https://discourse.processing.org/u/goldorak)\
**Post date:** [July 13, 2021, 4:04pm UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/1 "2021-07-13T16:04:06Z")

</div>

Hi,

Instead of iterating through my loop with a classic iterator (when I say classic I mean iterating the loop using a fixed value with something like += 20).  
I would like to iterate with a value which is increasing or decreasing as I progress through my loop iteration.

Thanks for your help

---

<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:** [July 13, 2021, 4:24pm UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/2 "2021-07-13T16:24:22Z")

</div>

Hello @goldorak  
Interesting question…

I’m not sure about details of implementation but I would start by **trying to add a changing variable** to the standard regular +=20.

**For example: i = i + 20 + n**

**With the n variable being assigned a changing number** via random (or other) at each iteration…

🤓

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 14, 2021, 12:05am UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/3 "2021-07-14T00:05:06Z")

</div>

Hello, @goldorak, and welcome to the Processing Forum!

Are you looking for something like this?

```auto
void setup() {
  for (int n = 0, incr = 1; n <= 10; n += incr, incr += 1) {
    println(n);
  }
}

```

Output:

```
0
1
3
6
10

```

_EDIT (July 13, 2021):_

Also try this:

```auto
void setup() {
  for (int a = 0, b = 1, c;
           a <= 144;
           c = a, a = b, b = c + a)
  {
    println(a);
  }
}

```

Does the sequence that is output look familiar?

---

<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 14, 2021, 9:42am UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/4 "2021-07-14T09:42:59Z")

</div>

> [@goldorak](#):
>
> I would like to iterate with a value which is increasing or decreasing as I progress through my loop iteration.

You need to make sure the loop reaches a terminal value if the accumulative increases is close to the accmulative decreases then the loop might not end for a very long time. For instance I ran this sketch

```auto
int idx = 1;
for(int v = 0; v >= -100 && v <= 100; v += 5 * cos(random(TWO_PI))){
  println(idx++, v);
}

```

and it took 433954 iterations to stop but this varied a lot because of the random number. Changing it so we have `v += 5 * cos(0.75 + random(TWO_PI))` reduces it a lot because positive values predominate.

---

<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:** [July 14, 2021, 12:29pm UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/5 "2021-07-14T12:29:20Z")

</div>

> [@javagar](#):
>
> ```auto
> for (int n = 0, incr = 1; n <= 10; n += incr, incr += 1) {
> println(n);
> }
> 
> ```

* * *

this might be easier to understand.  
You don’t have to change the actual for-loop variable i.

```auto
  for (int i = -110; i<111; i++) {
    println(n);
    float value = i*i*i; 
  }

```

---

<div class="post-metadata">

**Author:** ![javagar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/javagar/32/11434_2.png) [@javagar](https://discourse.processing.org/u/javagar)\
**Post date:** [July 15, 2021, 10:12am UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/6 "2021-07-15T10:12:22Z")

</div>

Agreed, @Chrisir.

My examples were intended to answer the original poster’s query by demonstrating that the increment in a for loop can vary. The examples should have been accompanied by a caveat.

In general, the header of a loop should be a simple control structure, with the bulk of the functionality contained in the body of the loop.

The second example was inspired by this code from the official [Python](https://www.python.org/) web site:

```auto
def fib(n):
    a, b = 0, 1
    while a < n:
        print(a, end=' ')
        a, b = b, a+b
    print()
fib(1000)

```

As we can see, the work was performed, quite appropriately, in a while loop. But that algorithm presented an opportunity to demonstrate what could, but not necessarily what should, be done in a Java for loop header. In my example, all of the work except the output was performed in a for loop header.

So, with the caveats having been stated above, let’s carry that example to an even further extreme. Here is the loop again, with all the work contained in a header, and with no loop body at all:

```auto
void setup() {
  for (int a = 0, b = 1, c;
           a <= 144;
           c = a, a = b, b = c + a, println(c));
}

```

Caveat: don’t program in this manner. 😉

---

<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:** [July 15, 2021, 11:38am UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/7 "2021-07-15T11:38:17Z")

</div>

see also here for another approach

see [How do I lerp colors on a curve? (not linear interpolation) - #3 by Chrisir](https://discourse.processing.org/t/how-do-i-lerp-colors-on-a-curve-not-linear-interpolation/24815/3)

---

<div class="post-metadata">

**Author:** ![goldorak](https://avatars.discourse-cdn.com/v4/letter/g/dec6dc/32.png) [@goldorak](https://discourse.processing.org/u/goldorak)\
**Post date:** [July 19, 2021, 3:50pm UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/8 "2021-07-19T15:50:39Z")

</div>

Thank you everyone for sharing your knowledge this is very much appreciated.

---

<div class="post-metadata">

**Author:** ![KaseyBear](https://avatars.discourse-cdn.com/v4/letter/k/3ab097/32.png) [@KaseyBear](https://discourse.processing.org/u/KaseyBear)\
**Post date:** [July 20, 2021, 9:22am UTC](https://discourse.processing.org/t/how-to-non-linear-iterator/31235/9 "2021-07-20T09:22:28Z")

</div>

> [@Chrisir](#):
>
> see also here for another approach
> 
> see [How do I lerp colors on a curve? (not linear interpolation) - #3 by Chrisir and](https://discourse.processing.org/t/how-do-i-lerp-colors-on-a-curve-not-linear-interpolation/24815/3)[employee monitoring](https://www.worktime.com/employee-monitoring)

Thank you, Chrisir, for a useful guide
