Weird behaviour of an 'if' statement

Hi,
I’m trying to control an RGB diode (common anode) with this code:

#define RED 9
#define GREEN 10
#define BLUE 11

void setup() {
  pinMode(RED,OUTPUT);
  pinMode(GREEN,OUTPUT);
  pinMode(BLUE,OUTPUT);
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  Serial.begin(9600);

}

void loop() {
  color(0,255,255);
  delay(5000);
}

void color(char r,char g, char b)
{
  
  if(r>255||r<0||b<0||b>255||g<0||g>255)
  {
    
    
    Serial.write('r');
    return;
    
  }
  analogWrite(RED,r);
  analogWrite(GREEN,g);
  analogWrite(BLUE,b);
}

I have a strange problem. In the ‘color’ function, if I remove the if statement, everything works as expected(doesn’t return). If I remove the check for ‘g’, it works as expected. But if the check for ‘g’ is there, it returns.Why does that happen? I don’t really need that ‘if’ statement, just curious.
Thanks in advance!

Squeeee! Code tags with your first posting, +1 Karma.

Because g is less than zero. (-1 is less than zero)

If I was going to filter the range “(r>255||r<0) || (b<0||b>255) || (g<0||g>255)” I’d go about it a bit differently

( (r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >=0 && b <= 255) )

of course if you the programmer is not going to send values out of the range of 0-255, you might save a few clock ticks by not doing any checking for value range.

Oh, almost forgot. Look at the if statement with the ||'s and now that they are grouped you should be able to spot the error.

char is signed 8-bit on the Arduino Uno and similar. Can never be greater than 127 or less than -128.

byte is unsigned 8-bit if that's what you meant.

MarkT:
char is signed 8-bit on the Arduino Uno and similar. Can never be greater than 127 or less than -128.

byte is unsigned 8-bit if that's what you meant.

Thanks!