# Looping logical operators?

**URL:** <https://discourse.processing.org/t/looping-logical-operators/23267>\
**Category:** Coding Questions\
**Created:** [August 14, 2020, 11:01pm UTC](https://discourse.processing.org/t/looping-logical-operators/23267 "2020-08-14T23:01:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eltrem](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eltrem/32/3023_2.png) [@eltrem](https://discourse.processing.org/u/eltrem)\
**Post date:** [August 14, 2020, 11:01pm UTC](https://discourse.processing.org/t/looping-logical-operators/23267/1 "2020-08-14T23:01:12Z")

</div>

Hi all!  
I’m wondering if there’s a way to evaluate operators in a loop… ooops…i hope this statement makes any sense…

maybe it is better if i show my code first:

```auto
for(int i=0; i<height; i++){
      if((scalenotes.hasValue(i)==true)||(scalenotes.hasValue(i-1)==true)||(scalenotes.hasValue(i+1)==true)){         
      }
      else{
        line(0,i,width,i); 
      }
    }

```

So,  
i have filled an IntList with a bunch of values, and i’m comparing this values using a for loop. I’m painting a horizontal line whenever the result of the comparison is false.  
I used the || operator to compare also the precedent and subsequent index that the for loop is pointing to.

And now i’m wondering how could i extend this comparison for an arbitrary number of precedent and subsequent index(from i-n to i+n), without having to write by hand all the || operator cases.

Most probably my overall approach is wrong,  
and there might be something which i ignore and that would make my life easier?

thanks for your unvaluable help!

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [August 15, 2020, 2:58am UTC](https://discourse.processing.org/t/looping-logical-operators/23267/2 "2020-08-15T02:58:07Z")

</div>

> [@eltrem](#):
>
> And now I’m wondering how could I extend this comparison for an arbitrary number of precedent and subsequent index (`from i-n to i+n`), without having to write by hand all the `||` operator cases.

I’ve written a util function named **containsAnyOfRange()** which you can invoke from within your loop, hopefully removing much of its boilerplate: 😉

```auto
// https://Discourse.Processing.org/t/looping-logical-operators/23267/2
// GoToLoop (2020/Aug/15)

final IntList il = new IntList(new int[] { -3, 2, 4, 20, 41 });

void setup() {
  println(il);
  println(containsAnyOfRange(il, -5, 0)); // true
  println(containsAnyOfRange(il, 0, -5)); // false
  println(containsAnyOfRange(il, 35, 45)); // true
  println(containsAnyOfRange(il, 7, 19)); // false
  println(containsAnyOfRange(il, 7, 20)); // true
  println(containsAnyOfRange(il, 20, 20)); // true
  exit();
}

static final boolean 
  containsAnyOfRange(final IntList il, int low, final int high) {
  if (il != null) while (low <= high) if (il.hasValue(low++)) return true; 
  return false;
}

```

---

<div class="post-metadata">

**Author:** ![eltrem](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/eltrem/32/3023_2.png) [@eltrem](https://discourse.processing.org/u/eltrem)\
**Post date:** [August 15, 2020, 4:51pm UTC](https://discourse.processing.org/t/looping-logical-operators/23267/3 "2020-08-15T16:51:06Z")

</div>

Amazing!  
I just had to do this:

```auto
final IntList il = new IntList(new int[] { 60, 72, 84, 96 });

void setup() {
  size(120,120);
  noLoop();
}

void draw(){
  background(0);
  stroke(255);
  strokeWeight(1);
  int weight=2;
  for(int i=0;i<height;i++){
    if (containsAnyOfRange(il, i-(weight/2), i+(weight/2))){
      line(0,i,width,i);
    }
  }  
}

static final boolean 
  containsAnyOfRange(final IntList il, int low, final int high) {
  if (il != null) while (low <= high) if (il.hasValue(low++)) return true; 
  return false;
}

```

And it worked exactly the way i was looking for…

Thank you very much!
