Mail Archives: djgpp/1997/08/10/22:37:11
On Thu, 07 Aug 1997 17:15:11 GMT, xfesenko AT pacific DOT net DOT sg (Victor) wrote:
>Is it safe to assume that the expression
>
>if(first_function()==OK && second_function()==OK &&
>third_function()==OK) return(OK);
>
>will be evaluated from left to right?
>
>Regards.
>Victor Fesenko
Yes, its called "Short Circuit Evaluation". In an ANDing expression, the
expressions will be evaluated from left to right UNTIL one returns false.
I say this because, in your example, some functions may not be evaluated at
all and that could be a surprise if your are not expecting it.
The best example of this is "if (p && *p == 'a') {}" where the NULLness of
p would be checked in the first expression before deferencing it in the
second expression.
Although not guaranteed, most C's stop evaluation once an ORing expression
returns true from one of its expressions. Another potential surprise.
...Kenneth
- Raw text -