Logical Operators

Hi all,

Are the following statements the same?

1
2
3
int x(5);
if((x == 4) || (x == 30))
   ++x;
and
1
2
3
int x(5);
if(x == (4 || 30))
   ++x;
Of course not. || takes two boolean operands and evaluates to true if at least one of them is true.
When converting integers to bool, all non-zero values are converted to true. When doing the reverse, true becomes 1 and false 0.

Therefore the second is equivalent to
if (x==1)++x;
Topic archived. No new replies allowed.