Tuesday, January 26, 2010

OR operator in expression

Logical OR(||) operator expression doesn't check second operand, if the first operand is true. i.e., d=a > b||b > c. In this expression if first operand is true then no need of checking second operand. Because though second operand is true or not, the expression will be true.
consider this program:
#include< stdio.h >
int a=1,b=2;
if(a < b++||++a < b)
printf("%d %d",a,b);

1 3

in this program a < b++(1 < 2 post increment so b will increment after checking) is true. So the second operand ++a < b isn't checked by OR . Now a=1,b=3 Because of second operand is not checked a won't be incremented.


Post a Comment