Short way to add if statements to while loop.

Apr 8, 2022 at 7:51am
Hey everyone,

I'm lookin for a way to add statements to my while loop

Now I have this:


 
  while (it.getNextEvent(currentMessage, samplePos))


Now i want to add to this: if x !=0 and bool MidiMessage::isForChannel(x)
...

So the while checks for incoming midi messages, then i want to check if the user has selected (in the GUI on a slider) a different midichannel and if that channel corresponds with the channel of the incoming midi message.

I want to learn how to write this elegantly in stead of writing nested if statements
Last edited on Apr 8, 2022 at 7:51am
Apr 8, 2022 at 7:59am
Take the word “bool” out and you can probably write exactly what you have there. You can replace “and” by && if you wish, but it’s not obligatory.
Apr 8, 2022 at 8:07am
can i put it in the same line as the while?
Apr 8, 2022 at 8:12am
Yes, you can “and” three requirements together within the while test. It will be equivalent to nested ifs in the same order.
Apr 8, 2022 at 8:42am
ok, when implementing i realized that i want to execute the while loop:

always if x= 0

if x > 0 and Midimessage.isForChannel(x) also

And if x differs from Midimessage.isForChannel(x) don't execute the while loop

Don't know if my explanation is ok
Apr 8, 2022 at 9:16am
Possibly for the first 2 requirements:

 
while (x == 0 || (x > 0 && Midimessage.isForChannel(x)))


How does the 3) reconcile with 1) ?? 1) says always if x == 0 but 3) says don't execute if x != Midimessage.isForChannel(x) ??

3) can be re-phrased as execute if x == Midimessage.isForChannel(x).

So do you mean:

 
while (x == 0 || (x > 0 && Midimessage.isForChannel(x)) || ( x == Midimessage.isForChannel(x)))


???

Apr 8, 2022 at 10:02am
it works! tx!
Topic archived. No new replies allowed.