Conditional Statements
1. Conditional Statements
In this chapter, you already learned about relational operators, which tell us how R objects relate, and logical operators, which allow us to combine logical values. Now R also provides a way to use the results of these operators to change the behavior of your own R scripts. Sure enough, I'm talking about the if and else statements here.2. if statement
Have a look at the recipe for the if statement: The if statement takes a condition; if the condition evaluates to TRUE, the R code associated with the if statement is executed. The condition to check appears inside parentheses, while the R code that has to be executed if the condition is TRUE, follows in curly brackets. Let's have a look at an example. Suppose we have a variable x equal to -3. If this x is smaller than zero, we want R to print out "x is a negative number!". How can we do this using the if statement? We first assign the variable, x and then write the if test. If we run this bit of code, we indeed see that the string "x is a negative number" gets printed out.3. if statement
However, if we change x to 5, and re-run the code, the condition will be FALSE, the code is not executed, and the printout will not occur.4. else statement
This brings us to the else statement: this conditional statement does not need an explicit condition; instead, it has to be used together with an if statement. The code associated with an else statement gets executed whenever the condition of the if test is not satisfied. We can extend our recipe by including an else statement as follows. Returning to our example, suppose we want to print out "x is positive or zero", whenever the condition is not met. We can simply add the else statement. If we run the code with x equal to -3, we still get the printout "x is a negative number", because the if condition is TRUE.5. else statement
However, if we now change x to 5, the text "x is either a positive number or zero" is printed out; the x smaller than zero condition was not satisfied, so R turned to the expression in the else statement.6. else if statement
There are also cases in which you want to customize your programs even further. Maybe we want yet another printout if x equals exactly 0. How to do this? Well, R also provides the else if statement. Let's first extend the recipe. The else if statement comes in between the if and else statement. To see how R deals with these different conditions and corresponding code blocks, let's first extend our example.7. else if statement
Say we want R to print out "x is zero" if x equals 0 and to print out "x is a positive number" otherwise. We add the else if, together with a new print statement, and adapt the message we print on the else statement. How does R process this control structure? Let's first go through what happens when x equals -3. In this case, the condition for the if statement evaluates to TRUE, so "x is a negative number" gets printed out, and R ignores the rest of the statements.8. else if statement
If x equals 0, R will first check the if condition, sees that it is FALSE, and will then head over to the else if condition. This condition, x == 0, evaluates to TRUE, so "x is zero" gets printed to the console, and R ignores the else statement entirely.9. else if statement
Finally, what happens when x equals 5? Well, the if condition evaluates to FALSE, so does the else if condition, so R executes the else statement, printing "x is a positive number".10. if, else if, else
Remember that as soon as R stumbles upon a condition that evaluates to TRUE, R executes the corresponding code and then ignores the rest of the control structure. This becomes important if the conditions you list are not mutually exclusive. Have a look at this example, that sees if a number is divisible by 2 or by 3. When x equals 6, the first condition evaluates to TRUE, so R prints out "divisible by 2". Now R exits the control structure and will not look at the rest of the statements. So although the second condition, for the else if part, would evaluate to TRUE, nothing gets printed out.11. Let's practice!
Create Your Free Account
or
By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.