Differencebetween Short (&,|) and Long (&&, ||) Forms of And, or Logical Operators in R

Boolean operators && and ||

The shorter ones are vectorized, meaning they can return a vector, like this:

((-2:2) >= 0) & ((-2:2) <= 0)
# [1] FALSE FALSE TRUE FALSE FALSE

The longer form evaluates left to right examining only the first element of each vector, so the above gives

((-2:2) >= 0) && ((-2:2) <= 0)
# [1] FALSE

As the help page says, this makes the longer form "appropriate for programming control-flow and [is] typically preferred in if clauses."

So you want to use the long forms only when you are certain the vectors are length one.

You should be absolutely certain your vectors are only length 1, such as in cases where they are functions that return only length 1 booleans. You want to use the short forms if the vectors are length possibly >1. So if you're not absolutely sure, you should either check first, or use the short form and then use all and any to reduce it to length one for use in control flow statements, like if.

The functions all and any are often used on the result of a vectorized comparison to see if all or any of the comparisons are true, respectively. The results from these functions are sure to be length 1 so they are appropriate for use in if clauses, while the results from the vectorized comparison are not. (Though those results would be appropriate for use in ifelse.

One final difference: the && and || only evaluate as many terms as they need to (which seems to be what is meant by short-circuiting). For example, here's a comparison using an undefined value a; if it didn't short-circuit, as & and | don't, it would give an error.

a
# Error: object 'a' not found
TRUE || a
# [1] TRUE
FALSE && a
# [1] FALSE
TRUE | a
# Error: object 'a' not found
FALSE & a
# Error: object 'a' not found

Finally, see section 8.2.17 in The R Inferno, titled "and and andand".

&& (AND) and || (OR) in IF statements

No, it will not be evaluated. And this is very useful. For example, if you need to test whether a String is not null or empty, you can write:

if (str != null && !str.isEmpty()) {
doSomethingWith(str.charAt(0));
}

or, the other way around

if (str == null || str.isEmpty()) {
complainAboutUnusableString();
} else {
doSomethingWith(str.charAt(0));
}

If we didn't have 'short-circuits' in Java, we'd receive a lot of NullPointerExceptions in the above lines of code.

Are || and ! operators sufficient to make every possible logical expression?

Yes, as the other answers pointed out, the set of operators comprising of || and ! is functionally complete. Here's a constructive proof of that, showing how to use them to express all sixteen possible logical connectives between the boolean variables A and B:

  • True: A || !A
  • A NAND B: !A || !B
  • B implies A: !B || A
  • A implies B: !A || B
  • A OR B: A || B
  • Not B: !B
  • Not A: !A
  • A XOR B: !(!A || B) || !(A || !B)
  • A XNOR B: !(!A || !B) || !(A || B)
  • A: A
  • B: B
  • A NOR B: !(A || B)
  • A does not imply B: !(!A || B)
  • B does not imply A: !(!B || A)
  • A AND B: !(!A || !B)
  • False: !(A || !A)

Note that both NAND and NOR are by themselves functionally complete (which can be proved using the same method above), so if you want to verify that a set of operators is functionally complete, it's enough to show that you can express either NAND or NOR with it.

Here's a graph showing the Venn diagrams for each of the connectives listed above:

Sample Image

[source]

If with multiple &&, || conditions Evaluation in java

If I understand you correctly, in the first part of your question, you are asking whether a and b must both be true for the entire expression (a && b || c || d || e) to evaluate as true.

The answer to that question is no. Java operators are such that && has higher precedence than ||. So the equivalent expression is:

(a && b) || c || d || e

Therefore the expression as a whole will evaluate to true if any of a && b or c or d or e is true. For example, if a was false and c was true, the expression as a whole is true.

Note that Java conditional operators short-circuit. This means that once the end result of the expression is known, evaluation stops. For example, if a and b were both true, meaning the overall expression must evaluate to true, then c, d and e are not evaluated.

For the second part of your question, we can apply the same logic. The equivalent expression is therefore:

(a && b) || (a && c) || (a && d) || (a && e)

This will evaluate to true if any of the sub-components, eg. a && c evaluates to true. As others have noted, a must be true for the expression to be true as it is part of every sub-component. Then if any of the other variables is true, the expression is true.

Once you understand that, you can see how the simplification of this expression suggested by @Arc676 is arrived at:

a && (b || c || d || e)

I should also add that the two expressions in your question are different logically. They are equivalent to:

(a && b) || c || d || e

and

a && (b || c || d || e)

The parentheses affect the order of evaluation. In the first, a and b are grouped together by an &&, hence both must be true for that part of the expression (in the parentheses) to be true. In the second, b, c, d and e are grouped together by ||. In this case, only one of b, c, d and e needs to be true for that part of the expression (in the parentheses) to be true.

Using nested if statements vs logical operator && in c++

For the first example, if pieceisnotonedge returns false, will it also check the next condition?

No. It will "short-circuit" because if the first condition is false, checking the conditions after it is unnecessary. Read more here and here.

This is guranteed by the C++ standard:

7.6.14

... && guarantees left-to-right evaluation: the second operand is not evaluated if the first operand is false.

Note that, for || this is opposite, that is, if the first condition is "true", then checking the conditions afterwards is unnecessary

Shall i use; or...

Both are same, if you have a short if statement (with only two conditions), I would suggest using the first approach. In terms of efficiency there is no difference and you can verify this by looking at the generated assembly for both cases on godbolt

What is &&&& operation in C

The use of labels as values is a gcc extension (see here). Your expression segment:

c = i &&&& i;

equates to:
c = i && (&&i);
where &&i is the address of the label i.

Keep in mind you're combining two totally different i "objects" here. The first is the i variable which cycles through 0, 1, 2, while the second is the label i, for which the address is always some non-zero value.

That means that the result placed in C will be 0 (false) only when the variable i is 0. That's why you're getting the 0, 1, 1 sequence.

As an aside, I give serious thoughts to "employee management practices" if one of my minions bought me code like this for production use. Anything that removes the possibility of monstrosities like this would be a good thing in my opinion :-)



Related Topics



Leave a reply



Submit