Conditional Expressions and Operators

DESCRIPTION:

Conditionally evaluates expressions. Either an if expression or an if- else expression is possible.

USAGE:

if(test) true.expr 
if(test) true.expr else false.expr 
e1 || e2 
e1 && e2 

REQUIRED ARGUMENTS:

test
logical expression of length 1.
e1
logical expression of length 1.
e2
logical expression of length 1.

VALUE:

The if construct in the language evaluates test. If it is TRUE (i.e., if the first element of the result coerced to mode logical is TRUE), then true.expr is evaluated and returned as the value of the whole expression. Otherwise, the value of the expression is false.expr if present, or an empty value otherwise.

&& returns TRUE if both of its operands are TRUE (in the same sense as test above). If the left operand is not TRUE, the right operand is not evaluated.

|| returns TRUE if one of its operands is TRUE (in the same sense as test above). If the left operand is TRUE, the right operand is not evaluated.

DETAILS:

An NA value for the test causes an error.

A zero length logical vector causes an error if passed as the left operand for || or passed as either operand for &&.

The && and || operators are members of the Ops group of generic functions.

SEE ALSO:

for element-wise logical operations (|, &, ! and xor); for parallel selection of values; and for multi-branch selections.

EXAMPLES:

if(mode(x)!="character" && min(x)>0) log(x) 
  # log(x) is an error if mode(x) is "character" 
if(is.na(okay)) z <- z+1 else if(okay) {y <- rnorm(1); x <- runif(1)}