Solution Review: if/else Statements
In this review, we provide a detailed analysis of the solution to this problem.
We'll cover the following...
We'll cover the following...
Solution: Using a Combination of if and else
R
testVariable <- 45if(testVariable %% 3 == 0 && testVariable %% 5 == 0){cat("foo bar")} else {if(testVariable %% 3 == 0){cat("foo")}if(testVariable %% 5 == 0){cat("bar")}}
Explanation
We first check whether the testVariable is a multiple of both and . For this, we use an if condition
testVariable %% 3 == 0 && testVariable %% 5 == 0
The modulus sign %% helps us figure out whether the testVariable is a multiple of the number. The remainder after division of testVariable with would be if it is a multiple of . The remainder after division of testVariable with would also be if it is a multiple of .
We then AND the results of both the tests.
If this condition is not satisfied, we move towards the else block. In the else block we have two more if conditions: one for only multiple of and one for checking only multiple of .