Nested If vs. Compound If

bpayne111

Junior Contributor
Joined
Feb 28, 2003
Messages
325
Location
BFE
Is a compound If Statement faster than a nested If?

ie.

If x>y AndAlso x>z then
'do someting
end if

or

If x>y then
If x>z then
'do something
End If
End If
 
Probabbly the same, probabbly justt better to do the first type its much neater, i've never been a fan of nested if's i try to keep 2 levels the maximum i use in code (of course in practice subs calling subs calling subs is lost of nesting!) :)
 
Second one should be faster since in the first example both comparisons will always be checked, even if first one is false
 
Ups. Yeah. You're quite right.

While "And" means short circuit computing "AndAlso" will always force the second term to ba evaluated in any case.

So, what bpayne probably meant was "And" anyway.

Thanks for pointing that out!!
 
That's backwards. The AndAlso operator is the new support for short circuit comparisons. And will evaluate both, AndAlso won't. Heiko's original response is likely accurate, but I must say I think AndAlso is much more elegant than nesting.
 
Okay, i'm totally confused now.

Is "AND" the short circuit operator? or is it "ANDALSO"?
And (also) am I right about the meaning?

If (booleanFunction1) AND (booleanFunction2) then

If (booleanFunction1) ANDALSO (booleanFunction2) then

In the "short circuit" version, booleanFunction2 will not be evaluated. Right?

H
 
AndAlso was introduced to provide VB developers with Short Circuit evalutation. "And" wasn't used becuase it'd confuse VB6 developers who expect both expressions to be evaluated. The documentation provides a nifty little table for both keywords to show you what is evaluated when and what the whole expression will evaluate to.
 
i'll put it in laymans terms...

using And or Or will make all operations evaluate reguardless of the value ie (if the first statement is true the second will still evaluate even though it's not needed)

using AndAlso or OrElse will stop the evaluation as soon as True is returned and continue to the next line of code. (hence speeding up your code)

AndAlso and OrElse have thier downsides...
ie. Say the Second part of your evaluation is a Function that changes a variable and you need that value reguardless of the outcome of your statment.

So be careful... 99% of the time AndAlso is the way to go but sometimes you need to stick to the old way... this is why MS didn't just get rid of the original And and Or bitwise operators. (or just change them all together.

i think i'm gonna mark this day on my calender... i taught divil something yay.
 
bpayne111 said:
AndAlso and OrElse have thier downsides...
ie. Say the Second part of your evaluation is a Function that changes a variable and you need that value reguardless of the outcome of your statment.
I'd say if someone gets this "downside" it's a result of poor coding technique rather than AndAlso/OrElse. I think it's generally a bad idea to have functions that change stuff in your expressions.
 
point taken... i personally can't think of an example either but it's the concept that's important i think.

LONG LIVE ANDALSO lol
 
I can't think of an example where you *wouldn't* want short-circuiting, but I can think of a number of cases where you *would*. For instance, I do this all the time:
C#:
if(var!=null && (int)var > 5)
...

The "old" way of doing it required nesting, as in:
C#:
if(var!=null)
    if((int)var > 5)

-Nerseus, giving an unrequested opinion :)
 
Back
Top