Multiple || in for loop?

Hello arduinians :slight_smile:

I got my self an UNO on saturday and I’ve had some fun learning a shed loads about electronics and this is comming from an IT technician as it is my day job :slight_smile:

I’m still in the beginner stages so I’m working with simple circuits still, in my current project I have 2 RGB leds and I wish to light up 2 colors on each so figured I’d modify my first bliking white code just blink ( or at least at this stage keep them on for testing.
my code was this
for(int i = 0; i < 6; i++)

and I changed it to this so I could basiclly ignore the green pins in my project.

void loop() {
  //I want 0 2 3 & 5
  for(int i = 0; i < 6 && i == 0 || 2 || 3 || 5; i++) 
   digitalWrite(ledpins[i], 0);

I’m using 6 pins on PWM ( which I will later play with for fading) which goes to each color so my var ledpins is an array of 6 refering to each pins and 0 2 3 5 are red and blue.

I uploaded this code but it just light all of them up as if it ignores my OR operators but I’m guessing I’m using || wrong. I’ve tried a serach but havnt found any similar for loop



i == 0 || 2 || 3 || 5

i == 0 is a comparison but 2 3 5 all evaluate to true, because they are all non-zero. So, you expression evaluates to i == 0 || true || true || true and true OR x is always true.

A much simpler approach is to have an array for each of the pins controlling each colour:

const int greenPin [] = {0, 2, 3, 5};

and then a simple for loop to step through.

for(int i = 0; i < 6 && i == 0 || i == 2 || i == 3 || i == 5; i++)

I tried this also assuming that was my mistake but it still the same result

ahh I see

I think you've misunderstood what the condition in a for loop is there for.

It defines the condition for sustaining (or terminating) the loop - it's a binary proposition, and as soon as the condition is false, the loop terminates.

I understand that now. thank you :D