Conformity with other languages, OrElse

Posted Mon, Apr 24 2006 6:29 by bill
I was just reading another one of Billy's blog entries, but this time I really have to disagree:
 
Billy said : " At first, VB was to be brought into conformity with other languages on how AND and OR clauses are handled in If statements. That is, given the condition "x AND y", then if x is False, it's not necessary to evaluate y."
 
That's not actually correct.  Other languages, at least of the C family have two distinct set of operators, & and |, and && and ||.  The first two evaluating both operands, the second two providing short circuiting.
 
So the language equivalents are:

And ->   &
Or   ->   |
AndAlso -> &&
OrElse   ->  ||
 
So you can see in both languages the short circuited operators are actually double words.  Hopefully that ends the fallacy of the flux being to do with conformity with other languages.  It never was.
 
 
 
 
 
Filed under: , ,

Comments

# re: Conformity with other languages, OrElse

Monday, April 24, 2006 3:09 PM by Ayende Rahien

Actually, no.
& - BITWISE AND
| - BITWISE OR

There is a distinct difference.

There is no such thing in C based langauges as non short cicuting operator.

# re: Conformity with other languages, OrElse

Monday, April 24, 2006 8:19 PM by bill

Hi Ayende,

Actually, no your wrong there. J#, C# both define & and | as being both bitwise and logical operators.
So they are both identical to VB's And and Or respectively.

From the C# help file:
--------------
Binary & operators are predefined for the integral types and bool. For integral types, & computes the logical bitwise AND of its operands. For bool operands, & computes the logical AND of its operands; that is, the result is true if and only if both its operands are true.

The & operator evaluates both operators regardless of the first one's value.
---------------------

# re: Conformity with other languages, OrElse

Monday, April 24, 2006 8:43 PM by Billy Hollis

I certainly don't claim to be an expert in C-based languages, but I'm pretty sure C only has short-circuited logical operators. I have not done enough C# to notice a difference, and just assumed it was the same. As Bill, points out, it's not (as I just checked out). So C# and C differ in that area.

My original assertion was based on what I was told in 2001 by a VB team member - that conformity was one of the driving factors in the change. It's certainly possible that he was mistaken.

# re: Conformity with other languages, OrElse

Monday, April 24, 2006 8:57 PM by bill

hey Billy,

It's the same in Java as well, so it's not just C#, J# and VB :)

I'm not sure who it was that told you it was for conformity, but hey, at least you'll know next time not to necessarily take what they say on face value ;)

# re: Conformity with other languages, OrElse

Monday, April 24, 2006 11:05 PM by bill

Oh, and I do want to point out it is really the same in C++ as C++ isn't really a strongly typed language, so a bitwise operation on booleans is actually the same as a logical operation that is not short circuited.

Just to be sure I did actually test this with a win32 mean and lean console app.

--------------
int _tmain(int argc, _TCHAR* argv[])
{
if (test1() & test2())
{
printf("hello world");
}
return 0;
}

bool test1()
{
printf(" test 1 \n");
return false;
}


bool test2()
{
printf(" test 2 \n");
return false;
}
--------------------

it does not print "hello world", which is what we would expect, but it does call both test1 and test2 even though test1 returns a bool with the value of false.

SO, there you have it folks, it is the same in C++, Java, J#, C# and VB.NET