And' VS '&&' as Operator

AND' vs '&&' as operator

If you use AND and OR, you'll eventually get tripped up by something like this:

$this_one = true;
$that = false;

$truthiness = $this_one and $that;

Want to guess what $truthiness equals?

If you said false... bzzzt, sorry, wrong!

$truthiness above has the value true. Why? = has a higher precedence than and. The addition of parentheses to show the implicit order makes this clearer:

($truthiness = $this_one) and $that

If you used && instead of and in the first code example, it would work as expected and be false.

As discussed in the comments below, this also works to get the correct value, as parentheses have higher precedence than =:

$truthiness = ($this_one and $that)

What is the difference and what are the & and && operators used for?

The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.

Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.

Sample Image

So for instance in the example you provided you have:

if(0 & 1){
}

The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.

The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.

if (!a && b < 5) {
}

The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.

So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.

Is there any difference between and and && operators in PHP?

In your case, && and and do the same thing, but take a look at the operator precedence. You can see that && and and are not on the same level, so mixing that could give you unexpected results in some cases - I recommend always using &&, but that's your choice.

Difference between && and and operators

What is the difference between the && and and operators?

There is none1. The "alternative" aspect of these operators means that they can be used to construct the exact same expressions from a semantic perspective.

Where and when do I use and over &&?

This is largely a matter of preference. I'm too used to && to not use it, but can understand if someone finds and more readable.

why does C++ introduce alternative operators?

C++ was designed to be available on a variety of character sets and platforms. Trigraphs, like Bathsheba pointed out, are another example of such a feature. If a character set would not allow && to be written (say, because it simply didn't have the & character) then one can still get by with the alternative representation. Nowadays, it's largely moot.



1 Actually, upon further thinking, my answer to your first question can be refined. There is a slight lack of equivalence, pertaining to how tokens are parsed. && doesn't require a space to be parsed as a separate token, while and does. That means:

void foo(bool b1, bool b2) {
if(b1&&b2) { // Well formed
}

if(b1andb2) { // ill formed, needs spaces around `and`
}
}

What is the difference between & and && in Java?

& <-- verifies both operands

&& <-- stops evaluating if the first operand evaluates to false since the result will be false

(x != 0) & (1/x > 1) <-- this means evaluate (x != 0) then evaluate (1/x > 1) then do the &. the problem is that for x=0 this will throw an exception.

(x != 0) && (1/x > 1) <-- this means evaluate (x != 0) and only if this is true then evaluate (1/x > 1) so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1).

EDIT:

exprA | exprB <-- this means evaluate exprA then evaluate exprB then do the |.

exprA || exprB <-- this means evaluate exprA and only if this is false then evaluate exprB and do the ||.

Difference between | and || , or & and &&

The operators |, &, and ~ act on individual bits in parallel. They can be used only on integer types. a | b does an independent OR operation of each bit of a with the corresponding bit of b to generate that bit of the result.

The operators ||, &&, and ! act on each entire operand as a single true/false value. Any data type can be used that implicitly converts to bool. Many data types, including float implicitly convert to bool with an implied !=0 operation.

|| and && also "short circuit". That means whenever the value of the result can be determined by just the first operand, the second is not evaluated. Example:

ptr && (*ptr==7) If ptr is zero, the result is false without any risk of seg faulting by dereferencing zero.

You could contrast that with (int)ptr & (*ptr). Ignoring the fact that this would be a bizarre operation to even want, if (int)ptr were zero, the entire result would be zero, so a human might think you don't need the second operand in that case. But the program will likely compute both anyway.

What is the difference between && and ||?

You are looking at a "C++ puzzle" which is using two languages tricks.

The first is that postincrement uses the value of the variable first, then increments the variable.

So x++ has the value 2 in the expression and then x becomes 3
y++ has the value 0 in the expression, and then y becomes 1

&& is and
|| is or

both operators are short-circuiting.

Look at && first.

In order for and to be true, both sides must be true (non-zero)

Since it is x++ && y++, first the x++ happens, and since it is non-zero (true)
the y++ has to happen to determine whether the result is true or not.
Therefore x is 3 and y is 1.

The second case is the same. y is zero, but in order to determine if the OR is true,
the if statement executes the second half of the expression.

The third case, since it is in the opposite order, never executes y++ because

x++ || y++

the first half being x++, it is already true, so the compiler does not bother to execute the second half of the test.



Related Topics



Leave a reply



Submit