# A better way to write this IF statement

**URL:** <https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322>\
**Category:** Coding Questions\
**Created:** [April 30, 2020, 7:35pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322 "2020-04-30T19:35:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![normalnatural](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@normalnatural](https://discourse.processing.org/u/normalnatural)\
**Post date:** [April 30, 2020, 7:35pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/1 "2020-04-30T19:35:22Z")

</div>

Hi there,  
I’m very new to processing and I was wondering if there’s a better way to write this IF statement. What I’m doing is creating a drawing tool and want to confine the line to a space in the window. This statement works but I’m guessing there’s a cleaner way to write this out and would love to know how.

Thanks!

```auto
    if (mouseX > margin && mouseX < width - margin && mouseY > margin && mouseY < width - margin && pmouseX > margin && pmouseX < width - margin && pmouseY > margin && pmouseY < width - margin) {
      line(pmouseX,pmouseY,0, mouseX,mouseY, 0);
    }

```

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [April 30, 2020, 8:06pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/2 "2020-04-30T20:06:08Z")

</div>

if(statements can be written as follows

```auto
if(condition){
   someCode();
}

if(condition)someCode();

//note this is best kept to single instructions for the condition as it can fail if you chain more than one instruction and you would then have to encapsulate everything in {}

```

A traditional if-else construct in C, Java and JavaScript is written:

```auto
if (a > b) {
    result = x;
} else {
    result = y;
}

```

This can be rewritten as the following statement:

`result = a > b ? x : y;`

---

<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:** [April 30, 2020, 8:17pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/3 "2020-04-30T20:17:28Z")

</div>

One thing you might think of is in fame number ‘fn’  
`mouseX > margin && mouseX < width - margin && mouseY > margin && mouseY < width - margin`  
is the same as this  
`pmouseX > margin && pmouseX < width - margin && pmouseY > margin && pmouseY < width - margin`  
in frame no ‘fn + 1’

So if you have 2 global boolean variables `curr` and `last` then this should work

```auto
curr = mouseX > margin && mouseX < width - margin && mouseY > margin && mouseY < width - margin;
if ( curr && last) {
      line(pmouseX,pmouseY,0, mouseX,mouseY, 0);
}
last = curr;

```

Note this reduces the number of boolean conditions to evaluate by half 😀

---

<div class="post-metadata">

**Author:** ![normalnatural](https://avatars.discourse-cdn.com/v4/letter/n/cab0a1/32.png) [@normalnatural](https://discourse.processing.org/u/normalnatural)\
**Post date:** [April 30, 2020, 8:20pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/4 "2020-04-30T20:20:25Z")

</div>

Thank you. That’s really helpful to think of the arguments as variables.

---

<div class="post-metadata">

**Author:** ![KevinWho](https://avatars.discourse-cdn.com/v4/letter/k/dbc845/32.png) [@KevinWho](https://discourse.processing.org/u/KevinWho)\
**Post date:** [May 1, 2020, 1:29am UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/5 "2020-05-01T01:29:48Z")

</div>

This appears to be code for a button, you could make a method the returns a bool based on if the mouse is in that area.

Example function:

```auto
boolean inArea(int X, int Y, int x, int y, int w, int h) {
  if (X > x && Y > y && X < x + w && Y < y + h) {
    return true;
  } else {
    return false;
  }
}

```

For this case just put  
if(inArea(mouseX,mouseY,rectX,rectY,rectWidth,rectHeight)){}  
if this is code for a button add mousePressed to that

---

<div class="post-metadata">

**Author:** ![Java.SourceForger](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/java.sourceforger/32/9831_2.png) [@Java.SourceForger](https://discourse.processing.org/u/Java.SourceForger)\
**Post date:** [May 2, 2020, 4:35pm UTC](https://discourse.processing.org/t/a-better-way-to-write-this-if-statement/20322/6 "2020-05-02T16:35:57Z")

</div>

If you want you can separate each logic statement in the `if()` By a line break:

```auto
if (mouseX > margin 
&& mouseX < width - margin 
&& mouseY > margin 
&& mouseY < width - margin 
&& pmouseX > margin
&& pmouseX < width - margin 
&& pmouseY > margin
&& pmouseY < width - margin) {
      line(pmouseX,pmouseY,0, mouseX,mouseY, 0);
    }

```
