The if statement is used when you want to execute a command under certain conditions. A condition is checked, and if it is true (TRUE
), the specified operation is performed.
The logic of the simple if statement is illustrated in the diagram:
Let’s consider the following example: We have a student’s motivation level and want to output the comment very motivated (a vector mot
with a single value) if the student has indicated a value greater than or equal to \(7\) on the scale (\(0\)-\(10\)).
mot <- 9
if (mot >= 7) {
print("sehr motiviert")
}
So, the simple if statement follows a clear pattern:
if is called with the condition in parentheses: if (mot >= 7)
The instructions to be executed if the condition is true are enclosed in curly braces: { print("very motivated") }
The simple if statement can be easily implemented.
{ /* examples */ }
In addition to the simple if statement, there are also else if statements that allow you to check additional conditions if the previous condition is not met. In this case, the process checks multiple conditions. This is illustrated in the diagram:
For another scenario where a student has only indicated a motivation score of \(5\), we could write the following else if statement:
mot <- 5
if (mot >= 7) {
print("sehr motiviert")
} else if (mot >= 4 & mot < 7) {
print("motiviert")
}
This means that the simple if syntax is only extended here by the following points:
another statement that checks an additional condition with else if: else if (mot >= 4 & mot < 7)
an additional block of instructions for the additional condition: { print("motivated")}
This can also be implemented very easily. The important thing here is: The second condition is only checked if the first condition is FALSE
. You can program as many else if
checks as needed. Let’s consider another example:
mot <- 3
if (mot >= 7) {
print("sehr motiviert")
} else if (mot >= 4 & mot < 7) {
print("motiviert")
} else if (mot >= 0 & mot < 4) {
print("nicht motiviert")
}
The only change we made here is adding a third condition check. As before, this third condition is only checked if both the first and second conditions are FALSE
. else if statements have a hierarchical structure.
You can also make dichotomous decisions. For example, let’s say we want to differentiate only between motivated and not motivated individuals. We decide to classify individuals who have a score above \(7\) as motivated and those with lower scale values as not motivated. Here, we only check the condition if the value is greater than \(7\). If this is not the case, we decide with else (in all other cases) to output not motivated. Else contains an instruction that is executed for all cases where the previous if condition is not met (i.e., results in FALSE
).
mot <- 5
if (mot >= 7) {
print("sehr motiviert")
} else {
print("nicht motiviert")
}
Here the structure is quite simple as well:
we add another statement after the if condition with else, which does not have a condition anymore: else
. Remember: With else, the condition is inherent, as it includes all FALSE
cases of the previous condition.
Here again, we specify the statement in curly braces: { print("not motivated)}
These were simple examples of if and else if statements. Typically, these are used within loops (e.g., when making decisions for all observations within a dataset). This will be shown on the next page. ```