The use of the if…else conditional operator
The general structure of an if…else
statement is as follows:
if (conditional_expression) { body_of_task } else { body_of_task2 }
The general structure is clearly intuitive to understand. If the conditional expression produces a result that is TRUE
, then it will execute the body_of_task
section, and if the conditional expression is FALSE
, then it will execute the body_of_task2
section. In this recipe, you will implement a small task to implement the if…else
statement.
Getting ready
Unlike the ifelse
function, if…else
works for conditioning on a scalar-valued input and output. The ifelse
function takes the input as a vector and produces a vector output with the same length as that of the input. However, if…else
takes only a scalar-valued input and it only generates TRUE
or FALSE
and then executes the body of task that you define. Let’s assume you have a number stored in the variable a
and you want to test whether the given...