On Mon, May 22, 2006 at 08:36:39PM -0400, Ryan Flannery wrote:
> On 5/22/06, prad <[EMAIL PROTECTED]> wrote:
> >this is not openbsd specific, but i wanted to ask people who really 
> >understand
> >the inner workings of programming languages.
> >
> >suppose that you have 2 conditions A and B where B take a lot of effort to
> >determine (eg looking for a string match in a huge file).
> >
> >either A or B needs to be true before you can execute 'this'.
> >
> >the 2 if statements below are equivalent i think:
> >
> >if A or B:
> >        do this
> >
> >if A:
> >        do this
> >elseif B:
> >        do this
> >
> >now, do they work the same way?
> >
> >in the second if A is true we don't need to go looking for B (the more
> >laborious one).
> >
> >in the first, do both A and B get evaluated or does A get evaluated first
> >(because it is first in sequence) and if it is true, no evaluation of B 
> >takes
> >place?
> >
> >do all programming and shell languages handle this the same way?

The best thing to do, in any language, is to state what you mean
clearly. If you know you want A evaluated first because it's cheap, then
do it that way and comment it.

// if (A or B), but check A first because B is expensive
if A:
  something
elseif B:
  something
end if

This works whether short circuit evaluation is in effect or not, and
it's clear that you mean it to work that way and why.

-- 
Darrin Chandler            |  Phoenix BSD Users Group
[EMAIL PROTECTED]   |  http://bsd.phoenix.az.us/
http://www.stilyagin.com/  |

Reply via email to