OR, AND Operator

Alexry picture Alexry · May 17, 2010 · Viewed 166.5k times · Source

Newbie question. How to calculate the value of the formula A f B, where f - the binary function OR or AND?

Answer

Martin Randall picture Martin Randall · May 17, 2010

There is a distinction between the conditional operators && and || and the boolean operators & and |. Mainly it is a difference of precendence (which operators get evaluated first) and also the && and || are 'escaping'. This means that is a sequence such as...

cond1 && cond2 && cond3

If cond1 is false, neither cond2 or cond3 are evaluated as the code rightly assumes that no matter what their value, the expression cannot be true. Likewise...

cond1 || cond2 || cond3

If cond1 is true, neither cond2 or cond3 are evaluated as the expression must be true no matter what their value is.

The bitwise counterparts, & and | are not escaping.

Hope that helps.