Simple Boolean logic 'not working'

I have a long program with boolean logic that doesn't seem to work so I condensed the problem into this for testing:

void setup()
{
  Serial.begin(9600);
  Serial.println(F("Begin"));
  int sw1 = 1;
  int sw2 = 0;
  boolean Err = true;
  if (sw1 == 1 || sw2 == 1 && Err == false)
  {
     Serial.println(F("Condition met"));
   }
}

void loop()
{  
}

In essence, if either switch 1 or switch 2 (or both) are on and error flag is false, then I want a certain action to take place (print "Condition met" in the example above). Changing the switch variables achieves the anticipated effect however whether Err equals true or false seems to have no effect on the algorithm.

I usually run into trouble mixing my 'AND's and 'OR's.
Can someone explain the flaw in my logic?

Need extra parens ?

Do you mean

if ((sw1 == 1 || sw2 == 1) && Err == false)

Seems to work.

Thanks.

Sometimes you need even go as fas as

if (( (sw1 == 1) || (sw2 == 1)) && (Err == false))

In this table you can see the && is executed befor the ||

In this table you can see the && is executed befor the ||

Which sort-of makes sense because && is a boolean equivalent of multiply, and || is a boolean equivalent of add.
(0*1 = 0, 0+1 = 1)
But it's better to just use extra parens than to count on everyone remembering the correct order of execution!

definitely! not only better, far easier to remember too :wink:

The MISRA-C and MISRA-C++ rules used by many developers of critical embedded software forbid expressions such as "sw1 == 1 || sw2 == 1 && Err == false" (without extra parentheses) for exactly this reason.

if you want to read more about MISRA-C / C++