And vs AndAlso, Or vs OrElse
-
Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance
-
Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance
Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance
AndAlso and OrElse perform what is known as short curcuit evaluation. What this means is that if the first argument makes it such that the result is known then the second argument is not tested. For example if you have:
If DayIsTuesDay() And DayisSomeonesBirthday()
Both DayIsTuesday() and DayIsSomeonesBirthday() will be valuated. However if you have:
If DayIsTuesDay() AndAlso DayisSomeonesBirthday()
and DayIsTuesday returns false then there is no point executing DayIsSomeonesBirthday Where your second operation is doing something slow (like reading from a database) using short curcuit evaluations can significantly speed up your application.
'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
-
Hi, what is the difference between those and which is faster? example: a=2 b,c,d,e=1 1)if a=1 and b=1 and c=1 and d=1 and e=1 then.... 2)if a=1 andAlso b=1 andAlso c=1 andAlso d=1 andAlso e=1 then.... 3)if a=10 or b=10 or c=10 or d=10 or e=1 then.... 4)if a=10 orElse b=10 orElse c=10 orElse d=10 orElse e=1 then.... Thanks in advance
I think the subliminal message is: "use
C#
!"If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
AndAlso and OrElse perform what is known as short curcuit evaluation. What this means is that if the first argument makes it such that the result is known then the second argument is not tested. For example if you have:
If DayIsTuesDay() And DayisSomeonesBirthday()
Both DayIsTuesday() and DayIsSomeonesBirthday() will be valuated. However if you have:
If DayIsTuesDay() AndAlso DayisSomeonesBirthday()
and DayIsTuesday returns false then there is no point executing DayIsSomeonesBirthday Where your second operation is doing something slow (like reading from a database) using short curcuit evaluations can significantly speed up your application.
'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
You may have to be a bit careful though. In the statement
If DayIsTuesDay() And DayisSomeonesBirthday()
if "DayIsTuesday" alters data, and "DayIsSomeonesBirthday" also alters data, you might not want to short-cut... for example (and yes, its a crap example!)
if ClearUserTable() and ClearPasswordTable() then
messagebox.show("Tables Cleared")
end ifIn this case, you would definitely NOT want to shortcut the second function using AndAlso
-
I think the subliminal message is: "use
C#
!"If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]The VB.Net keyword
And
is equivalent to the C# operator & The VB.Net keywordAndAlso
is equivalent to the C# operator && The VB.Net keywordOr
is equivalent to the C# operator | The VB.Net keywordOrElse
is equivalent to the C# operator || The subliminal message is RTM[^]?'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
-
Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
This is in no way different from the way C# works with &[^] and &&[^]...
'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
-
The VB.Net keyword
And
is equivalent to the C# operator & The VB.Net keywordAndAlso
is equivalent to the C# operator && The VB.Net keywordOr
is equivalent to the C# operator | The VB.Net keywordOrElse
is equivalent to the C# operator || The subliminal message is RTM[^]?'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
Duncan Edwards Jones wrote:
The subliminal message is RTM[^]?
Nope. IMHO default behaviour should be short-cut and the keywords
AndAlso
,OrElse
are simply foolish, hence my subliminal still stands. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
The VB.Net keyword
And
is equivalent to the C# operator & The VB.Net keywordAndAlso
is equivalent to the C# operator && The VB.Net keywordOr
is equivalent to the C# operator | The VB.Net keywordOrElse
is equivalent to the C# operator || The subliminal message is RTM[^]?'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
Right. Read The Fantastic Manual. Apparently no one does anymore, even asking Google seems too much for some. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
-
Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
Nice explanation about AndAlso. However, proper checks for the division by zero in the example could do away with the need for AndAlso. It does make sense about short circuiting if the boolean condition can be determined with out fully evaluating the entire expression.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
Right. Read The Fantastic Manual. Apparently no one does anymore, even asking Google seems too much for some. :)
Luc Pattyn [Forum Guidelines] [My Articles]
Voting for dummies? No thanks. X|
Luc Pattyn wrote:
Google seems too much for some
Seems like it.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
-
This is in no way different from the way C# works with &[^] and &&[^]...
'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ).
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
-
The VB.Net keyword
And
is equivalent to the C# operator & The VB.Net keywordAndAlso
is equivalent to the C# operator && The VB.Net keywordOr
is equivalent to the C# operator | The VB.Net keywordOrElse
is equivalent to the C# operator || The subliminal message is RTM[^]?'--8<------------------------ Ex Datis: Duncan Jones Merrion Computing Ltd
As a minor semantic difference - the & operator in C# is a bitwise and, whereas && is the logical and which also shortcircuits. (Like C++) I'm used to using & with caution - coming from a C++ background - you could have two "true" results from a function - but 1 & 2 == false ;) In C# this cautioning really doesnt apply though.
Mark Churchill Director Dunn & Churchill Free Download:
Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio. -
As a minor semantic difference - the & operator in C# is a bitwise and, whereas && is the logical and which also shortcircuits. (Like C++) I'm used to using & with caution - coming from a C++ background - you could have two "true" results from a function - but 1 & 2 == false ;) In C# this cautioning really doesnt apply though.
Mark Churchill Director Dunn & Churchill Free Download:
Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.There is no semantic difference. The following are *exactly* equivalent: VB: C#: And & Or | AndAlso && OrElse || The VB 'And' is *both* a bitwise operator and a non-short-circuiting logical operator, as is the C# '&' operator. Ditto for 'Or' and '|'. They produce identical results when used in the same situations. Obviously, it usually makes sense to use 'AndAlso' and 'OrElse' in most logical evaluation situations, but occasionally 'And' and 'Or' may be preferred, in exactly the same cases where '&' and '|' may be preferred in C#.
David Anton http://www.tangiblesoftwaresolutions.com C++ to C# Converter C++ to VB Converter C++ to Java Converter VB to Java Converter Java to VB & C# Converter Instant C#: VB to C# converter Instant VB: C# to VB converter Instant C++: convert VB, C#, or Java to C++/CLI
-
The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ).
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
>>The way I read it, it was saying that VB6 would evaluate the whole line even when it knew it was going to fail, so you can't do if (x != null && x.y != 0 ). Correct! VB6 would eval both expressions before coming back with a result, even if the first failed. AndAlso & OrElse allow "short cut" checks. Meaning after the first expression that fails/passes the check, the remaining checks are ignored and processing continues.
-
Wow - VB sure sucks. http://visualbasic.about.com/od/usingvbnet/l/bldykvbnetlogop.htm[^] Looks like the answer is that the old operators do not optimise in any way.
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )
The one that drives me nuts is the lack of a decent ternery operator. The rough equivalent of say: foo == null ? "null" : foo.bar; is Iif(foo = null, "null", foo.bar) Which is just a regular function, all arguments get evaluated before it's called, and bang! Null ref. :/
Mark Churchill Director Dunn & Churchill Free Download:
Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio. -
The one that drives me nuts is the lack of a decent ternery operator. The rough equivalent of say: foo == null ? "null" : foo.bar; is Iif(foo = null, "null", foo.bar) Which is just a regular function, all arguments get evaluated before it's called, and bang! Null ref. :/
Mark Churchill Director Dunn & Churchill Free Download:
Diamond Binding: The simple, powerful, reliable, and effective data layer toolkit for Visual Studio.Yeah, I've been bitten by that on brief visits to VB land
Christian Graus Please read this if you don't understand the answer I've given you "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )