Nested If vs. Compound If

bpayne111

Well-known member
Joined
Feb 28, 2003
Messages
326
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, ive never been a fan of nested ifs 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. Youre 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!!
 
Thats backwards. The AndAlso operator is the new support for short circuit comparisons. And will evaluate both, AndAlso wont. Heikos original response is likely accurate, but I must say I think AndAlso is much more elegant than nesting.
 
I have to agree with quwiltw, AndAlso and OrElse are more elegant and efficient. I use em where I can.
 
Okay, im 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" wasnt used becuase itd 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.
 
Ok, I actually had no idea AndAlso and OrElse existed.

/me sits in the corner
 
ill 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 its 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 didnt just get rid of the original And and Or bitwise operators. (or just change them all together.

i think im gonna mark this day on my calender... i taught divil something yay.
 
Originally posted by bpayne111
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.
Id say if someone gets this "downside" its a result of poor coding technique rather than AndAlso/OrElse. I think its generally a bad idea to have functions that change stuff in your expressions.
 
point taken... i personally cant think of an example either but its the concept thats important i think.

LONG LIVE ANDALSO lol
 
I cant think of an example where you *wouldnt* 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