As is the case with almost every other programming language, there is no escaping a conditional execution of certain blocks of code with R programming. With the if…else control structure, you can formulate conditions to control the execution of your program.
Possibilities for using if…else statements in R¶
In code, an if…else statement, or conditional branching, ensures that certain blocks of instructions only execute if a condition is met. If if…else statements are inseparable from most programs, it is because their conditional functionality is elementary. It is often necessary to use if…else conditions in the most complex algorithms.
Integrating if…else statements into the source code can, for example, be useful for testing user input on a specific value. However, if…else structures in R are primarily used with for loops. When searching for a specific value in a data structure, it is possible to use an if statement to stop the loop as soon as that value has been found.
Syntax of if…else statements in R¶
If…else statements in R can be used in a variety of ways because you can combine them differently the keywords “if”, “else” and “else if…”. However, the basic logic always remains the same: the execution of each block of code always depends on a condition.
The syntax does her too the subject of a rigorous definition. The “if” keyword is always followed by a condition specified in parentheses. Arithmetic or logical comparison operators are often used for this condition. Next comes a block of code enclosed in braces, which only executes if the specified condition applies. Depending on the purpose of the statement, it is possible to use the “else” keyword to introduce a block of code to be executed if the if condition does not apply. Additionally, other conditions can be defined using the “else if…” keywords.
If the blocks you want to run include only one line of code, curly braces are not needed for if…else statements in R. This can be helpful in terms of readability. Have you just started learning programming? However, we advise you to use parentheses by default; This allows you to protect your back.
if statement in R¶
Do you want to apply a specific conditional action to your code while continuing the standard execution of your program? For this you only needan if statement in R.
a <- 0
b <- 40
if (a == 0) {
print("Zero division not allowed")
stop()
}
c <- b / a
R
In the code example above, the number stored in variable b should be divided by the number stored in variable a. Since division by 0 is impossible, the if condition checks that the number stored in the variable a is not a 0. If this condition is met, the program executes the code in curly brackets, which allows you to terminate at runtime.
if…else statement in R¶
You wish execute blocks of code in case the specified condition is not met ? To do this, you can use an if…else statement. As in the previous example, you must start by defining an if condition. If this is not met, the program then moves to a block of code written specifically for such a case. Below is an example to illustrate this situation:
a <- 4
b <- 2
if (b < a) {
print("a is greater than b")
}
else {
print("a and b are equal or b is greater than a")
}
R
Two variables a and b are created again, each of which contains integer values. The if condition allows you to check if the value b is less than the value a. In such an eventuality, the instruction block executes within braces. In the opposite case, the program goes directly to the block of instructions following the “else” keyword and executes the lines of code it contains.
if…else statement in R¶
In some cases, you may want to check not one, but several conditions. All you have to do is use the keywords “else if…”. If you are familiar with if…else statements in Python then the “else if…” statement in R in its “elif” form may already have no secrets for you.
a <- 4
b <- 2
if (b < a) {
print("a is greater than b")
}
else if (b > a) {
print("b is greater than a")
}
R
This code example is not much different from the previous one; the “else” statement has only been replaced by an “else if…” statement. As with an “if” statement, the condition must be defined in parentheses after the “else if…” statement.
Attention : conditions specified by “else if…” are mutually exclusive. As is the case here, if the first condition is already met, the code introduced by “else if…” is not executed. Compared to several simple “if” statements, the use of “else if…” statements therefore saves your computing power.