Solution Review: Relational and Logical Operators
In this review, we give a detailed analysis of the solution to this problem.
Solution #1: Performing each Test Separately
testVariable <- 19test1 <- testVariable > 4test2 <- testVariable < 10result <- test1 && test2cat(result)
Explanation
Our task was to check whether the testVariable
lies between and . For that, we first check whether the number is greater than and store the result in a variable test1
. Then we check whether the number is less than and store the result in variable test2
. Later, we take the &&
of the two variables so that we can check whether both the tests pass or not and print the result.
Solution #2: Using One Boolean Expression
testVariable <- 19cat(testVariable > 4 && testVariable < 10)
Explanation
The above method is simpler. Just use relational and logical operators to make a boolean equation and print the result. R compiler is smart enough to compute the value of testVariable > 4
first then compute the value of testVariable < 10
and later AND their results. This is because the relational operators have higher precedence than logical operators. So, they are executed first and then the logical operators are applied.