[r] Boolean operators && and ||

According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former is vectorized while the latter is not.

According to the help text, I read the difference akin to the difference between an "And" and "AndAlso" (correspondingly "Or" and "OrElse")... Meaning: That not all evaluations if they don't have to be (i.e. A or B or C is always true if A is true, so stop evaluating if A is true)

Could someone shed light here? Also, is there an AndAlso and OrElse in R?

The answer is


The answer about "short-circuiting" is potentially misleading, but has some truth (see below). In the R/S language, && and || only evaluate the first element in the first argument. All other elements in a vector or list are ignored regardless of the first ones value. Those operators are designed to work with the if (cond) {} else{} construction and to direct program control rather than construct new vectors.. The & and the | operators are designed to work on vectors, so they will be applied "in parallel", so to speak, along the length of the longest argument. Both vectors need to be evaluated before the comparisons are made. If the vectors are not the same length, then recycling of the shorter argument is performed.

When the arguments to && or || are evaluated, there is "short-circuiting" in that if any of the values in succession from left to right are determinative, then evaluations cease and the final value is returned.

> if( print(1) ) {print(2)} else {print(3)}
[1] 1
[1] 2
> if(FALSE && print(1) ) {print(2)} else {print(3)} # `print(1)` not evaluated
[1] 3
> if(TRUE && print(1) ) {print(2)} else {print(3)}
[1] 1
[1] 2
> if(TRUE && !print(1) ) {print(2)} else {print(3)}
[1] 1
[1] 3
> if(FALSE && !print(1) ) {print(2)} else {print(3)}
[1] 3

The advantage of short-circuiting will only appear when the arguments take a long time to evaluate. That will typically occur when the arguments are functions that either process larger objects or have mathematical operations that are more complex.


&& and || are what is called "short circuiting". That means that they will not evaluate the second operand if the first operand is enough to determine the value of the expression.

For example if the first operand to && is false then there is no point in evaluating the second operand, since it can't change the value of the expression (false && true and false && false are both false). The same goes for || when the first operand is true.

You can read more about this here: http://en.wikipedia.org/wiki/Short-circuit_evaluation From the table on that page you can see that && is equivalent to AndAlso in VB.NET, which I assume you are referring to.


Examples related to r

How to get AIC from Conway–Maxwell-Poisson regression via COM-poisson package in R? R : how to simply repeat a command? session not created: This version of ChromeDriver only supports Chrome version 74 error with ChromeDriver Chrome using Selenium How to show code but hide output in RMarkdown? remove kernel on jupyter notebook Function to calculate R2 (R-squared) in R Center Plot title in ggplot2 R ggplot2: stat_count() must not be used with a y aesthetic error in Bar graph R multiple conditions in if statement What does "The following object is masked from 'package:xxx'" mean?

Examples related to logical-operators

Javascript logical "!==" operator? Java logical operator short-circuiting Boolean operators && and || Simple logical operators in Bash Logical Operators, || or OR? Differences in boolean operators: & vs && and | vs || Python's equivalent of && (logical-and) in an if-statement Logical operators ("and", "or") in DOS batch Logical XOR operator in C++? What's the difference between & and && in MATLAB?

Examples related to boolean-operations

Boolean operators && and || Using NOT operator in IF conditions OR, AND Operator

Examples related to or-operator

Boolean operators && and || Logical Operators, || or OR? What does the construct x = x || y mean? JavaScript OR (||) variable assignment explanation

Examples related to and-operator

Boolean operators && and || Regex AND operator Python's equivalent of && (logical-and) in an if-statement PHP: if !empty & empty