# Have a problem with "for" comand

**URL:** <https://discourse.processing.org/t/have-a-problem-with-for-comand/16337>\
**Category:** Coding Questions\
**Created:** [December 11, 2019, 10:01pm UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337 "2019-12-11T22:01:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 11, 2019, 10:01pm UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/1 "2019-12-11T22:01:02Z")

</div>

i’m trying to create a code that display a grid with squares and when you put the mouse over a square it disappears, how ever in my code when I put the mouse over a square the row and the column disappear…  
here is my code

```auto

int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX != lastEnteredX && posY != lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }

```

and also if you know a way to make them disappear for ever it would be great!

---

<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:** [December 11, 2019, 10:23pm UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/2 "2019-12-11T22:23:52Z")

</div>

> [@AkaiKenshi](#):
>
> a way to make them disappear for ever

Please read the tutorial about two-dimensional arrays

Basically you store whether a cell in the grid was clicked

If so, don’t show it or show it differently

---

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 11, 2019, 10:36pm UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/3 "2019-12-11T22:36:35Z")

</div>

Oh thanks, sorry if it seams like a dumb question, I’m just learning

---

<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:** [December 11, 2019, 10:58pm UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/4 "2019-12-11T22:58:10Z")

</div>

Oh no, it was an excellent question

The tutorial is very good

---

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 12, 2019, 4:51am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/5 "2019-12-12T04:51:08Z")

</div>

well I started reading what you said, but I could not stop thinking why it didn’t work the way I expected, so I did a little test… I change this lines

```auto
if (posX != lastEnteredX && posY != lastEnteredY) {

```

to

```auto
if (posX == lastEnteredX && posY == lastEnteredY) {

```

and it work as I expected, just the other way around do you know why?  
btw will keep reading what you told me it seams that fixes the disappears forever problem

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 12, 2019, 5:44am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/6 "2019-12-12T05:44:37Z")

</div>

Could you explain what you mean by

> [@AkaiKenshi](#):
>
> I could not stop thinking why it didn’t work the way I expected, so I did a little test… I change this lines
> 
> ```auto
> if (posX != lastEnteredX && posY != lastEnteredY) {
> 
> ```
> 
> to
> 
> ```auto
> if (posX == lastEnteredX && posY == lastEnteredY) {
> 
> ```
> 
> and it work as I expected, just the other way around do you know why?

I doubt that‘s what you were refering to, but if you meant why it was the other way around, that is because the opposite statement of == (equal) is !=(not equal).

---

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 12, 2019, 5:46am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/7 "2019-12-12T05:46:14Z")

</div>

try this code

```auto

int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX != lastEnteredX && posY != lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }

```

and then try this code

```auto

int objX;
int objY;
int x, y, w, h;
int lastEnteredX = 100000000;
int lastEnteredY = 100000000;
int posX;
int posY; 
void setup() {
  size(1000, 1000);
  x = 20;
  y = 20;
  h = 85;
  w = 85;
}
void draw() {
  background(#000000);
  name();
  objX = mouseX;
  objY = mouseY;
}

  void name() {
    for (int i = 0; i < (10*w +10); i = i + w + 10) {
      for (int j = 0; j < 5*h + 10; j += h + 10) { 
        posX = i;
        posY = j; 

        if (objX < x + w + i && objX > x + i && objY < y + h + j && objY > y + j) {
          println("entered, I: " + i + " J: "+ j + " lastEnteredX: " + lastEnteredX + ", lastEnteredY: " + lastEnteredY );
          lastEnteredX = i;
          lastEnteredY = j;
        }
        if (posX == lastEnteredX && posY == lastEnteredY) {
          rect(x + i, y + j, w, h);
        }
      }
    }
  }

```

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 12, 2019, 5:49am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/8 "2019-12-12T05:49:41Z")

</div>

Ah, i see. So the first Code is what you don‘t want. The second one is what you want, just the other way around.

Just put the whole condition into a bracket and negate it, like this :

```auto
if (!(posX == lastX && posY == lastY))

```

---

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 12, 2019, 5:53am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/9 "2019-12-12T05:53:21Z")

</div>

first thank you… second do you know why do this happen?

---

<div class="post-metadata">

**Author:** ![Lexyth](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/lexyth/32/7403_2.png) [@Lexyth](https://discourse.processing.org/u/Lexyth)\
**Post date:** [December 12, 2019, 6:16am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/10 "2019-12-12T06:16:59Z")

</div>

Yes, this happens, because there is a difference between ( !true && !true ) and ( !(true && true)). To demonstrate this, take a 3\*3 grid for example.

```auto
if ( x != 1 && y != 1) print(x, y); //where 1 would be the middle (0, 1, 2).length is 3

//this would print : 
// (0,0), ,(2,0)
// , ,
// (0,2), ,(2,2)

//while 
if (x == 1 && y == 1) 

//
// (1,1)
// 

//and then
if ( ! (x == 1 && y == 1)) //this just negates the result of the previously shown one. 
//Because ! is the sign for negation ( !true would be false 
//and this !showRect would not show a rect 
//and !notShowRect would show a rect)

//printed result would be 
//(0,0),(1,0),(2,0)
//(0,1), ,(2,1)
//(0,2),(1,2),(2,2)

```

And this probably doesn‘t explain it too well, but i only noticed that halfway through… Anyway, to explain it better, the && means that if one is false, the whole statement is regarded as false, because (true&&false) == false cause && means „are they both“, which they aren’t, while || on the other Hand would mean „is either of them“.

So if you had x = 1 and y = 0 and if ( x != 1 && y != 1 ) it’s the same as if ( false && true ), which (&& basically meaning „are both true?“) results in if (false) and thus not getting triggered even though you only wanted (1,1) to not trigger the if statement…

Now this also happens when using ==, just on a larger scale (on all except the one where both return true, thus triggering the if statement only with that one.

But what we wanted was the exact opposite, so just negating this all gives is what we were looking for.

---

<div class="post-metadata">

**Author:** ![AkaiKenshi](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@AkaiKenshi](https://discourse.processing.org/u/AkaiKenshi)\
**Post date:** [December 12, 2019, 6:20am UTC](https://discourse.processing.org/t/have-a-problem-with-for-comand/16337/11 "2019-12-12T06:20:27Z")

</div>

ohhhhh… thanks good to know, will apply it on further codes
