01 / 21
& & | |

Vectorized vs.
Short-Circuit Operators

Learn when R processes every element of a vector and when it stops early: the key distinction between & | and && ||.

R Programming

What Are Logical Operators?

Logical operators combine or negate TRUE and FALSE values. In R, they are the foundation of conditional logic: filtering data, controlling program flow, and writing tests.

AND
TRUE only when both sides are TRUE.
OR
TRUE when at least one side is TRUE.
NOT
Flips TRUE to FALSE and vice versa.

Vectors: R's Core Data Structure

Almost everything in R is a vector: a sequence of values of the same type. Even a single number like 5 is a vector of length 1.

TRUE FALSE TRUE TRUE FALSE A logical vector of length 5 c(TRUE, FALSE, TRUE, TRUE, FALSE)

Two Families of Logical Operators

Vectorized
& (AND) and | (OR) compare vectors element by element and return a vector of the same length.
Short-Circuit
&& (AND) and || (OR) examine only the first element and may skip evaluating the second operand entirely.

The Vectorized Rule: & and |

x TRUE FALSE TRUE & y FALSE TRUE TRUE element-by-element
\(T \land F = \text{FALSE}\)
\(F \land T = \text{FALSE}\)
\(T \land T = \text{TRUE}\)
Result FALSE FALSE TRUE

The Short-Circuit Rule: && and ||

Left operand FALSE && Right operand SKIPPED Left is FALSE → result is FALSE immediately. Right never runs. FALSE

Key Distinction at a Glance

Feature Vectorized (& |) Short-Circuit (&& ||)
Input length Any length vector Checks first element only
Output length Same as input Always length 1
Evaluates both sides? Always Only if needed
Primary use Data filtering, subsetting if/else conditions
Example context df[df$x \(> 0\) & df$y \(< 10\), ] if (\(n > 0\) && \(\frac{x}{n} > 0.5\))

Why Short-Circuiting Matters

Short-circuiting is not just an optimisation: it can prevent errors. If the left side of && is FALSE, the right side is skipped entirely, even if it would have caused a crash.

# Safe guard: avoid division by zero if (n != 0 && total / n > 0.5) { print("Above threshold") } # When n is 0, the right side never runs. # No error occurs!

How to Choose the Right Operator

1
Check your context. Are you inside an if() statement or filtering a vector/data frame?
2
if/else/while needs a single TRUE or FALSE → use && or ||.
3
Subsetting or vectorised operations (like filtering rows) → use & or |.
4
Need a safety guard? If the right side might error, use && / || so R can skip it when unnecessary.
5
Memory aid: single symbol = works on single elements in parallel (vectorised). Double symbol = checks one thing, then decides.

Example 1: Vectorized AND

# Two logical vectors x <- c(TRUE, FALSE, TRUE, TRUE) y <- c(TRUE, TRUE, FALSE, TRUE) x & y # [1] TRUE FALSE FALSE TRUE

Example 2: Short-Circuit AND

# Same vectors, double operator x <- c(TRUE, FALSE, TRUE, TRUE) y <- c(TRUE, TRUE, FALSE, TRUE) x && y # [1] TRUE # Only x[1] and y[1] are checked!

Example 3: Short-Circuit OR in Action

# || stops as soon as it finds TRUE a <- TRUE b <- stop("This would crash!") a || b # [1] TRUE # No error: b is never evaluated!
# Compare: vectorized | would crash a | b # Error: This would crash!

Example 4: Filtering a Data Frame

# Filter rows where age > 18 AND score > 80 students <- data.frame( name = c("Ali", "Bo", "Cat", "Dev"), age = c(17, 19, 20, 16), score = c(90, 85, 70, 95) ) students[students$age > 18 & students$score > 80, ] # name age score # 2 Bo 19 85

Decision Flowchart

Need logical operation? Working with a vector? YES & or | Vectorized NO && or || Short-Circuit Subsetting, filter, apply if(), while(), stopifnot()

Common Mistakes

✕ Using && in subset()
Using && inside bracket subsetting only checks the first row, silently returning wrong results.
✕ Using & in if()
Passing a vector to if() with & gives a warning: "the condition has \(\text{length} > 1\)." Use && instead.
✕ Ignoring recycling
If vectors differ in length, & and | recycle the shorter one silently, which can produce unexpected results.

Summary Table

Property & | && ||
Type Vectorized AND Vectorized OR Short-circuit AND Short-circuit OR
Elements checked All All First only First only
Return length \(n\) \(n\) 1 1
Short-circuits? No No Yes Yes
Best for Filtering Filtering if/while if/while

Question 1 of 5

What does c(TRUE, FALSE) & c(FALSE, TRUE) return?

Question 2 of 5

Which operator should you use inside an if() statement?

Question 3 of 5

What does FALSE && stop("error") produce?

Question 4 of 5

You want to filter a data frame to rows where age > 18 and score > 80. Which operator is correct?

Question 5 of 5

What is the key reason && and || are called "short-circuit" operators?