# Loop for same variable but opposite directions

**URL:** <https://discourse.processing.org/t/loop-for-same-variable-but-opposite-directions/36474>\
**Category:** Coding Questions\
**Created:** [April 20, 2022, 8:49am UTC](https://discourse.processing.org/t/loop-for-same-variable-but-opposite-directions/36474 "2022-04-20T08:49:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![oli7er](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/oli7er/32/16085_2.png) [@oli7er](https://discourse.processing.org/u/oli7er)\
**Post date:** [April 20, 2022, 8:49am UTC](https://discourse.processing.org/t/loop-for-same-variable-but-opposite-directions/36474/1 "2022-04-20T08:49:02Z")

</div>

Hi everyone,

I have been using two loops to generate generate variables one of them going up from 0 - 360 and the other going in the opposite direction from 360 - 0. The code I have been using for this looks like this:

```auto
      b = 0 + bSpeed;
      if (b > 360) {
        b = 359 - bSpeed;
      }
    
      b2 = 360 - bSpeed;
      if (b2 < 0) {
        b2 = 1 + bSpeed;
      }

```

Is there a simpler way to do this?

---

<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:** [April 20, 2022, 9:27am UTC](https://discourse.processing.org/t/loop-for-same-variable-but-opposite-directions/36474/2 "2022-04-20T09:27:50Z")

</div>

Hello,

Example:

```auto
int b = 0;
int dir = 1;
int bSpeed = 1;

void setup()
  {
  size(100, 100);
  frameRate(5); // Slow down for testing
  }

void draw() 
	{
  if(b>10) dir*= -1;
  if(b<0) dir*= -1;
    
  b += bSpeed*dir;
  
  println(b, bSpeed, dir); // Show in console
	}

```

`:)`

---

<div class="post-metadata">

**Author:** ![oli7er](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/oli7er/32/16085_2.png) [@oli7er](https://discourse.processing.org/u/oli7er)\
**Post date:** [April 20, 2022, 2:57pm UTC](https://discourse.processing.org/t/loop-for-same-variable-but-opposite-directions/36474/3 "2022-04-20T14:57:27Z")

</div>

Thank you brother this is exactly what I was looking for.
