Hi, I am new to vb.net. In using the If condition, can we
use either of And or AndAlso. Whats the difference ? In VB
I have always used And.
For eg.
if num > 10 AndAlso num < 20
Thanks.
Matt 22 3538
"Matt" <an*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** *****@phx.gbl.. . Hi, I am new to vb.net. In using the If condition, can we use either of And or AndAlso. Whats the difference ?
The "AndAlso" operator uses "short-circuit" evaluation - if the
First condition is False, the second isn't even evaluated. Useful
for things like this:
dataSet = FunctionThatRet urnsADataSet_Or Nothing()
' Did I get a DataSet with just one row in it?
If Not ( dataSet Is Nothing ) _
AndAlso dataSet.Tables( 0 ).Rows = 1 _
Then
"And" always works out /both/ expressions.
HTH,
Phill W.
*And* evaluates both Expression1 and Expression2. Whereas, *AndAlso*,
provides a means to perform short-circuiting the evaluation process is some
cases.
False AndAlso True will only evaluate the 'False' and knows it does not
need to evaluate the 'True' because it already has its answer. This can help
performance if used in a loop.
Regards - OHM
Matt wrote: Hi, I am new to vb.net. In using the If condition, can we use either of And or AndAlso. Whats the difference ? In VB I have always used And. For eg. if num > 10 AndAlso num < 20
Thanks. Matt
--
Best Regards - OHM
O_H_M{at}BTInte rnet{dot}com
In addition to the comments mentioned, there is also an "orelse" operator
which does lazy evaluation using the or operator
e.g.
If True orelse MyReallySlowFun ction() Then
MyReallySlowFun ction() will never get called because "orelse" has evaluated
True and so does not need to evaluate MyReallySlowFun ction()
HTH,
Trev.
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true only if x is true
regardless of the value of y, and if x is true and y is true.
Basically ANDALSO is the same as regular AND except that the first value
you are testing MUST be true to evaluate as true.
Is that confusing enough?
Matt wrote: Hi, I am new to vb.net. In using the If condition, can we use either of And or AndAlso. Whats the difference ? In VB I have always used And. For eg. if num > 10 AndAlso num < 20
Thanks. Matt
On 2004-01-09, copyco <co****@anon.co m> wrote: In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true only if x is true regardless of the value of y, and if x is true and y is true.
Umm, no. Read the rest of the thread for details, but if x is true
and y is false, ' x andalso y' will evaluate to false.
Basically ANDALSO is the same as regular AND except that the first value you are testing MUST be true to evaluate as true.
Again, no. (and this definition is different than your first
definition, but both are wrong).
Is that confusing enough?
Yes, which is why it's not done that way.
More specifically, 'x and y' and 'x andalso y' will always evaluate to
the same value for boolean values, either true or false.
Postscript: Actually, I suppose that's only true if evaluation order is
guaranteed with the 'And' operator, and I can't find anything that says
it is. So a minor caveat.
--
David
dfoster at
hotpop dot com
Just a useless trivia note:
The real terms for these operations are short-circuiting logical disjunction
(orelse), and short-circuiting logical conjunction (andalso), which most
people refer to as "short-circuited", or "lazy." In .NET it's used for
logical operators, but the concept is broad and could be applied to any
chain of logic.
It basically means, stop processing as soon as possible. Like playing
basketball best 3 out of 5, you don't need to actually play all five games
unless both teams win twice.
~
Jeremy
"Matt" <an*******@disc ussions.microso ft.com> wrote in message
news:01******** *************** *****@phx.gbl.. . Hi, I am new to vb.net. In using the If condition, can we use either of And or AndAlso. Whats the difference ? In VB I have always used And. For eg. if num > 10 AndAlso num < 20
Thanks. Matt
David wrote: On 2004-01-09, copyco <co****@anon.co m> wrote:
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true only if x is true regardless of the value of y, and if x is true and y is true.
Umm, no. Read the rest of the thread for details, but if x is true and y is false, ' x andalso y' will evaluate to false.
What you just said is what I said, or tried to say. Perhaps you didn't
understand what I wrote, or I didn't word it correctly. So we are in
agreement here.
Basically ANDALSO is the same as regular AND except that the first value you are testing MUST be true to evaluate as true.
Again, no. (and this definition is different than your first definition, but both are wrong).
According to the table given in the help file, what I said is correct.
The first value you are testing must be true in order for it to evaluate
to true regarless of the second value. And only when both values are
true does it evaluate to true. See the table below.
If expression1 is / And expression2 is / Value of result is
True True True
True False False
False (not evaluated) False
Is that confusing enough?
Yes, which is why it's not done that way.
More specifically, 'x and y' and 'x andalso y' will always evaluate to the same value for boolean values, either true or false.
Postscript: Actually, I suppose that's only true if evaluation order is guaranteed with the 'And' operator, and I can't find anything that says it is. So a minor caveat.
On 2004-01-09, copyco <co****@anon.co m> wrote:
David wrote:
On 2004-01-09, copyco <co****@anon.co m> wrote:
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true only if x is true regardless of the value of y, and if x is true and y is true.
Umm, no. Read the rest of the thread for details, but if x is true and y is false, ' x andalso y' will evaluate to false.
What you just said is what I said, or tried to say. Perhaps you didn't understand what I wrote, or I didn't word it correctly. So we are in agreement here.
Perhaps so, but why don't we rephrase your original statement?
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true if both x and y are true.
Period. End of discussion as far as the value of the operation goes.
But then what does this phrase fragment mean? In the case of "if x andalso y": this is true only if x is true regardless of the value of y...
Basically ANDALSO is the same as regular AND except that the first value you are testing MUST be true to evaluate as true.
Again, no. (and this definition is different than your first definition, but both are wrong).
According to the table given in the help file, what I said is correct.
No, the table's not the point here. Both 'And' and 'AndAlso' share
the same table. The problem above is the word "except", since in
both cases the first value MUST be true for the phrase to evaluate
as true. In other words, "the first value you are testing MUST
be true to evaluate as true" is true of both 'And' and 'AndAlso',
so the word "except" makes no sense here.
Now if you really understand this and there's just some grammatical
confusion, then no big deal. But you keep trying to point out
the And/AndAlso difference by saying something about the value
of 'X And Y' vs. 'X AndAlso Y', and that's just not the difference
between the two operators.
The first value you are testing must be true in order for it to evaluate to true regarless of the second value.
Likewise the second value must be true in order for it to evaluate to
true regardless of the first value.
Those two combined is simply is a very longwinded way of simply
saying...
only when both values are true does it evaluate to true.
And the lines above are true of the 'And' operator as well, so that
hardly points out the difference between them.
--
David
dfoster at
hotpop dot com
You're right. Now go have a cup of hot chocolate.
David wrote: On 2004-01-09, copyco <co****@anon.co m> wrote:
David wrote:
On 2004-01-09, copyco <co****@anon.co m> wrote:
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true only if x is true regardles s of the value of y, and if x is true and y is true.
Umm, no. Read the rest of the thread for details, but if x is true and y is false, ' x andalso y' will evaluate to false.
What you just said is what I said, or tried to say. Perhaps you didn't understand what I wrote, or I didn't word it correctly. So we are in agreement here.
Perhaps so, but why don't we rephrase your original statement?
In the case of "if x and y": this is true if both x and y are true.
In the case of "if x andalso y": this is true if both x and y are true.
Period. End of discussion as far as the value of the operation goes.
But then what does this phrase fragment mean?
In the case of "if x andalso y": this is true only if x is true regardles s of the value of y...
Basically ANDALSO is the same as regular AND except that the first value you are testing MUST be true to evaluate as true.
Again, no. (and this definition is different than your first definition , but both are wrong).
According to the table given in the help file, what I said is correct.
No, the table's not the point here. Both 'And' and 'AndAlso' share the same table. The problem above is the word "except", since in both cases the first value MUST be true for the phrase to evaluate as true. In other words, "the first value you are testing MUST be true to evaluate as true" is true of both 'And' and 'AndAlso', so the word "except" makes no sense here.
Now if you really understand this and there's just some grammatical confusion, then no big deal. But you keep trying to point out the And/AndAlso difference by saying something about the value of 'X And Y' vs. 'X AndAlso Y', and that's just not the difference between the two operators.
The first value you are testing must be true in order for it to evaluate to true regarless of the second value.
Likewise the second value must be true in order for it to evaluate to true regardless of the first value.
Those two combined is simply is a very longwinded way of simply saying...
only when both values are true does it evaluate to true.
And the lines above are true of the 'And' operator as well, so that hardly points out the difference between them. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Mike Hale |
last post by:
Is it better to use AndAlso and OrElse by default rather than the regual And
& Or?
MikeH
|
by: A Traveler |
last post by:
I was just curious if anyone knows how the combinations of And/AndAlso and
Or/OrElse compare in terms of performance.
Which takes more work for the system, performing two evaluations on an And
or performing short-circuiting on an AndAlso?
Purely for enlightenment.
Thanks in advance.
- Arthur Dent.
|
by: Lior |
last post by:
Hello .
I know that the AndAlso and OrElse statements are short-circuiting And and
Or statements ,
respectively .
Should I always use (I don't like the word "always" ...) AndAlso instead of
And
and OrElse instead of Or ?
|
by: =?Utf-8?B?UmljaA==?= |
last post by:
Greetings,
If x = y And m = n The
....
End If
If x = y AndAlso m = n Then
....
End If
|
by: Al G |
last post by:
Ok, so I've started to use it, and it is quicker.
Now, why is it that "AND" doesn't already work this way?
Al G
| |
by: Siegfried Heintze |
last post by:
Are there operators in C# that are the counterparts of "OrElse" and
"AndAlso" that I can use when translating the following program from VB.NET
to C#?
Thanks,
Siegfried
Namespace vbtest
Module Main
Function reflect(x as boolean) as boolean
|
by: Euvin |
last post by:
I am kind of confuse as to how these operators work:
AndAlso and OrElse
Anyone care to explain. Some examples would be great!
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
| |
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |