bpayne111 Posted March 26, 2003 Posted March 26, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
Heiko Posted March 26, 2003 Posted March 26, 2003 Both statements will probably result in the same assembler instructions anyway :) Quote .nerd
philprice Posted March 26, 2003 Posted March 26, 2003 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!) :) Quote Phil Price� Visual Studio .NET 2003 Enterprise Edition Microsoft Student Partner 2004 Microsoft Redmond, EMEA Intern 2004
vnarod Posted March 26, 2003 Posted March 26, 2003 Second one should be faster since in the first example both comparisons will always be checked, even if first one is false Quote
Heiko Posted March 26, 2003 Posted March 26, 2003 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!! Quote .nerd
Leaders quwiltw Posted March 26, 2003 Leaders Posted March 26, 2003 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. Quote --tim
Moderators Robby Posted March 26, 2003 Moderators Posted March 26, 2003 I have to agree with quwiltw, AndAlso and OrElse are more elegant and efficient. I use 'em where I can. Quote Visit...Bassic Software
Heiko Posted March 26, 2003 Posted March 26, 2003 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 Quote .nerd
Leaders quwiltw Posted March 26, 2003 Leaders Posted March 26, 2003 I have to agree with quwiltw... you make it sound so painful:p Quote --tim
Leaders quwiltw Posted March 26, 2003 Leaders Posted March 26, 2003 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. Quote --tim
*Gurus* divil Posted March 26, 2003 *Gurus* Posted March 26, 2003 Ok, I actually had no idea AndAlso and OrElse existed. /me sits in the corner Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
bpayne111 Posted March 26, 2003 Author Posted March 26, 2003 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. Quote i'm not lazy i'm just resting before i get tired.
Leaders quwiltw Posted March 26, 2003 Leaders Posted March 26, 2003 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. Quote --tim
bpayne111 Posted March 26, 2003 Author Posted March 26, 2003 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 Quote i'm not lazy i'm just resting before i get tired.
*Experts* Nerseus Posted March 26, 2003 *Experts* Posted March 26, 2003 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 :) Quote "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
Moderators Robby Posted March 26, 2003 Moderators Posted March 26, 2003 you make it sound so painful:p LOL Quote Visit...Bassic Software
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.