Looping logical operators?

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:

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!

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

// 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;
}
2 Likes

Amazing!
I just had to do this:

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!