Chapter 7 if else
statement
The if
statement only performs an operation if a condition is met. However, one may not only be interested in specifying the expression
that must be done when the required condition
is TRUE
, but also the expression
that must be performed when the condition
is FALSE
.
To achieve this, we can use the if() {} else {}
statement and ifelse()
function.
if(condition) {
1
expression else {
} 2
expression }
ifelse(condition,
1,
expression 2
expression )