Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

i'm not lazy i'm just resting before i get tired.
Posted
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!) :)

Phil Price�

Visual Studio .NET 2003 Enterprise Edition

Microsoft Student Partner 2004

Microsoft Redmond, EMEA Intern 2004

Posted

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!!

.nerd
  • Leaders
Posted
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.
--tim
Posted

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

.nerd
  • Leaders
Posted
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.
--tim
Posted

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.

i'm not lazy i'm just resting before i get tired.
  • Leaders
Posted
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.

--tim
Posted

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'm not lazy i'm just resting before i get tired.
  • *Experts*
Posted

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:

if(var!=null && (int)var > 5)
...

 

The "old" way of doing it required nesting, as in:

if(var!=null)
   if((int)var > 5)

 

-Nerseus, giving an unrequested opinion :)

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...