View Full Version : And / Or Logic in C
sweenster
06-11-2004, 01:25 PM
I never really use these double-queries much. can anyone tell me if this code is valid?
(If a=1 and b=1 OR a=1 and c=1 then...)
if (a=1 && b=1 || a=1 && c=1)
{
....
} else {
...
}
shmoove
06-11-2004, 03:33 PM
Looks almost right. You have to use "==" to compare instead of "=". "=" is only an assignment operator.
Also, I can never remember the precedence of the boolean operators so I never take my chances and use parenthesis (but I'm pretty sure && has higher precedence so they're probably not necessary:
if ((a==1 && b==1) || (a==1 && c==1))
shmoove
boris
06-11-2004, 03:39 PM
&& (AND) always has higher precedence than || (OR)
just so you know
and the code can be simplified
if(a == 1 && (b == 1 || c == 1))
ecnarongi
06-11-2004, 04:02 PM
for extra credit does anyone know the full precedence order??? :D
cpradio
06-11-2004, 05:48 PM
I know the precedence of the operators I use most often.
http://www.difranco.net/cop2220/op-prec.htm
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.