# Using and/or operators in R

In R, what is the difference between:

if(x>2 & x<3) ...


and

if(x>2 && x<3) ...


Similarly:

if(x<2 | x>3) ...


and

if(x<2 || x>3) ...


See ?"&": the single version does elementwise comparisons (for when you are doing logical operations on two vectors of the same length, e.g. if in your example x<-c(1.5,3.5). The other one works just like C++'s or java's &&: it only looks at the first element of each vector (this is typically an unexpected downside), but in a typically better performing way: it looks from left to right, and as soon as at least one of the values is false, it knows not to look at the rest anymore.
• +1; as an addition, it is nice to take a look at any() and all() in this context.