Author Topic: | and & vs || and &&?  (Read 1208 times)

i've noticed that most people use || and && in their code, but | and & work just the same
is this a coding habit from other languages or are they different in some way?

I think it's because == is the normal equals command while = tests if one thing CAN equal another, so it seems to be common sense.

== is actualy equals, while = is set, i don't think a single = tests anything

== is actualy equals, while = is set, i don't think a single = tests anything
Yes, it does.
Ex. containerRadiusSearches:
Code: [Select]
while(%storage = containerSearchNext)While there are objects that can be found by the search, while loop is true.
Code: [Select]
%a = 0;
while(%a = 1)
%a += 1;
infinite loop - and actually runs, because %a can be set to 1

Yes, it does.
Ex. containerRadiusSearches:
Code: [Select]
while(%storage = containerSearchNext)While there are objects that can be found by the search, while loop is true.
Code: [Select]
%a = 0;
while(%a = 1)
%a += 1;
infinite loop - and actually runs, because %a can be set to 1
actualy, X = Y returns Y overall
so...
$a = 0;
$b = 1;
$a = $b = 2;
would set everything to 2

in your while loop example, it's returning 1 which is true, and forms the infinite loop

edit: and echo($a = "hi"); echos hi

Yes, it does.
Ex. containerRadiusSearches:
Code: [Select]
while(%storage = containerSearchNext)While there are objects that can be found by the search, while loop is true.

First of all, it's containerSearchNext().
Secondly, if you do something like the following, it doesn't do an if check such as %pl == %cl.player, it sets %pl to %cl.player and then checks the value of %pl: if ( isObject( %pl = %cl.player ) )

A variable definition returns the value. Try it:
Code: [Select]
echo( $a = "hello" );
echo( $a SPC "world" );

| and & (along with << >> ^ and if Torque supports it, ~) are bitwise operators
http://www.cprogramming.com/tutorial/bitwise_operators.html
That link is for C/C++ but the syntax is the same thing


Yes, it does.
while(%storage = containerSearchNext())
== Always compares, it never sets
= Always sets, it never compares
Whats happening here (after I added (), bolded) is you are calling the function containerSearchNext, which will either return the next object, or a 0 if no object is found. %storage is then set to the value returned by containerSearchNext, then the while loop checks the value of %storage - a non-zero value results in it continuing the loop, zero (no object found) it will end the loop.
« Last Edit: March 11, 2012, 02:20:33 PM by Headcrab Zombie »



However, there is a more important difference, when just testing for booleans.
Logical or (||) and logical and (&&) short-circuit. This means that if it can figure out the value from the first part (first part is true for or, first part is false for and), the second argument will not even execute. Since the binary operators (| and &) have to support more than one bit, it's impossible to short-circuit reliably, which can be useful, even when only dealing with a single bit.

does it matter if things short circuit in BL though?
i don't see the point as much when it doesn't complain too much (ie, instead of stopping the program)

does it matter if things short circuit in BL though?
i don't see the point as much when it doesn't complain too much (ie, instead of stopping the program)

Code: [Select]
if (somethingThatMayFail() & somethingElseThatMayFailThatIsIndependent()) {
    // Bleh
}
vs
Code: [Select]
if (somethingThatMayFail() && somethingThatDependsOnTheFirstFunction()) {
    // Bleh
}


An example of the importance this has could be something like the following:
Code: [Select]
if ( isObject( %pl = %cl.player ) && ( !isObject( %mn = %pl.getMountedImage( 0 ) ) || %mn.getName() !$= "myWeaponImage" ) )
{
%pl.mountImage( myWeaponImage, 0 );
}

If && and || weren't short-circuit, this would cause console errors if there was no player or they were not holding something in slot 0.

An example of the importance this has could be something like the following:
Code: [Select]
if ( isObject( %pl = %cl.player ) && ( !isObject( %mn = %pl.getMountedImage( 0 ) ) || %mn.getName() !$= "myWeaponImage" ) )
{
%pl.mountImage( myWeaponImage, 0 );
}

If && and || weren't short-circuit, this would cause console errors if there was no player or they were not holding something in slot 0.
true, it does stop console errors, but it doesn't change the functionality, does it?

Yes, it does.
Ex. containerRadiusSearches:
Code: [Select]
while(%storage = containerSearchNext)While there are objects that can be found by the search, while loop is true.
Code: [Select]
%a = 0;
while(%a = 1)
%a += 1;
infinite loop - and actually runs, because %a can be set to 1
This is ridiculous. Absolutely ridiculous. NO. ITS NOT. YOU'RE WRONG.
== is to test if something equals something else
 =  is to SET SOMETHING to SOMETHING ELSE.

Oh my gosh.