# Why a= a\* -1 doesn't work here?

**URL:** <https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794>\
**Category:** Beginners\
**Created:** [June 19, 2021, 12:22pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794 "2021-06-19T12:22:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![karenChip](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenChip](https://discourse.processing.org/u/karenChip)\
**Post date:** [June 19, 2021, 12:22pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794/1 "2021-06-19T12:22:08Z")

</div>

Hi, I just made a rectangle which moves along the width and height of the window. I managed to make one but during the process, i encountered a problem that ‘a = a \* -1’ can’t work. I couldn’t figure it out the reason.  
here is the cold that works.

```auto
int x=0;
int y=0;
int xspeed;
int yspeed;

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

void draw(){
  background(0);
  
  //display a rect
  fill(255);
  rect(x,y,20,20);
  x = x + xspeed;
  y = y + yspeed;
  
  
  //state changing
  
  if(x == 0 && y == 0){
    xspeed = 1;
    yspeed = 0;
  }else if(x == 380 && y == 0){
    xspeed = 0;
    yspeed = 1;
  }else if(x == 380 && y == 380){
    xspeed = -1;
    yspeed = 0;
  }else if(x == 0 && y == 380){
    xspeed = 0;
    yspeed = -1;
   
  }
}

```

but if i change **int xspeed** into **int xspeed =1** and **xspeed = -1** into **xspeed = xspeed \* -1** , the rectangle would stuck there under that if statement. i don’t understand why it doesn’t work as the xspeed in both codes are equal to -1.  
here is the code with this problem.

```auto
int x=0;
int y=0;
int xspeed = 1;
int yspeed;

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

void draw(){
  background(0);
  
  //display a rect
  fill(255);
  rect(x,y,20,20);
  x = x + xspeed;
  y = y + yspeed;
  
  
  //state changing
  
  if(x == 0 && y == 0){
    xspeed = 1;
    yspeed = 0;
  }else if(x == 380 && y == 0){
    xspeed = 0;
    yspeed = 1;
  }else if(x == 380 && y == 380){
    xspeed = xspeed *-1;
    yspeed = 0;
  }else if(x == 0 && y == 380){
    xspeed = 0;
    yspeed = -1;
   
  }
}

```

What’s wrong with a= a\*-1 here?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [June 19, 2021, 12:36pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794/2 "2021-06-19T12:36:07Z")

</div>

0 \* -1 = 0

keep that in mind

---

<div class="post-metadata">

**Author:** ![karenChip](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenChip](https://discourse.processing.org/u/karenChip)\
**Post date:** [June 19, 2021, 12:37pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794/3 "2021-06-19T12:37:36Z")

</div>

but i already set int xspeed = 1 at the beginning of the code  
so it should be xspeed = 1 \*-1 = -1, not 0

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [June 19, 2021, 12:39pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794/4 "2021-06-19T12:39:14Z")

</div>

when the square comes to bottom right corner, its x speed is 0, when you tell it \*= -1, it will stay at 0

---

<div class="post-metadata">

**Author:** ![karenChip](https://avatars.discourse-cdn.com/v4/letter/k/50afbb/32.png) [@karenChip](https://discourse.processing.org/u/karenChip)\
**Post date:** [June 19, 2021, 12:40pm UTC](https://discourse.processing.org/t/why-a-a-1-doesnt-work-here/30794/5 "2021-06-19T12:40:38Z")

</div>

ahhh i got it now. it becomes 0 from the last if statement. Thank you so much!
