thingToTest <- "something"
if (thingToTest == "something") {
# Code to execute if condition is true
} else {
# Code to execute if condition is false
}
NULL
Decision Control Statements (like if/else statements) are used for making decisions in the program. They execute certain parts of the code based on whether a condition is true or false. They control the flow of execution based on decision-making but don’t inherently involve repetition or looping.
Decision control statements allow you to:
Adjust outputs or calculations based on changing data or user inputs.
Check conditions and manage potential errors or unexpected inputs.
Guide the flow of operations, ensuring appropriate steps are taken under specific circumstances.
thingToTest <- "something"
if (thingToTest == "something") {
# Code to execute if condition is true
} else {
# Code to execute if condition is false
}
NULL
An if/else statement contains:
an If
Statement that checks a condition and executes code if the condition is TRUE,
and optionally,
an Else
Statement that executes alternative code if the condition is false.
Here that condition would be TRUE because we know that thingToTest
is equal to “something”. So the code in the if
statement would be executed.
Here the double equals sign ==
is used to check if the value of thingToTest
is equal to “something”. This is different from the single equals sign =
which is used to assign a value to a variable, sometimes1
thingToTest <- "something"
thingToTest == "something" #TRUE
[1] TRUE
thingToTest == "something else" #FALSE
[1] FALSE
also works for numbers
thingToTest <- 5
thingToTest == 5 #TRUE
[1] TRUE
thingToTest == 6 #FALSE
[1] FALSE
and even logicals (this gets confusing)
thingToTest <- FALSE
thingToTest == FALSE #TRUE
[1] TRUE
thingToTest == TRUE #FALSE
[1] FALSE
The outputs of these tests are called Boolean or Logical variables. They can only have two values: TRUE
or FALSE
. Think of these like “yes” or “no” questions.
The term Logical is used in R, but Boolean is more common in other programming languages.
Logicals or Booleans can also be represented by the numbers 1 and 0, respectively.
This is because TRUE
is equivalent to 1 and FALSE
is equivalent to 0.
There are other comparison operators that can be used to check conditions. It is a good idea to commit these to memory if you find yourself using them alot.
for example, lets go back to an example with thingToTest
thingToTest <- 5
thingToTest > 4 # is 5 > 4? Yes
[1] TRUE
thingToTest < 4 # is 5 < 4? No
[1] FALSE
thingToTest >= 5 # is 5 > or = 5? Yes
[1] TRUE
thingToTest <= 5 # is 5 < or = 5? Yes
[1] TRUE
Suppose we have temperature data and want to categorize it as ‘Hot’ or ‘Cold’.
temperature <- 25 # Simulated temperature data
if (temperature > 25) { # If temperature is greater than 20
print("Hot") # Print "Hot"
} else { # Otherwise
print("Cold") # Print "Cold"
} # End if/else statement
[1] "Cold"
In this example, if temperature
is greater than 20, R will print “Hot”. If not, it will print “Cold”.
Nested If-Else: For more complex decisions, you can nest if-else statements within eachother.
for example, suppose we have exam scores and want to assign grades of either A, B, or C.
score <- 85 # Simulated exam score
if (score > 90) { # If score is greater than 90
grade <- "A" # Assign "A"
} else if (score > 75) { # If score is greater than 75
grade <- "B" # Assign "B"
} else if (score > 65) { # Otherwise (score is less than or equal to 75)
grade <- "C" # Assign "C"
} else {
grade <- "D"
}
print(grade)
[1] "B"
This code assigns a grade based on the score. It checks multiple conditions in sequence, demonstrating how nested if-else statements can handle more nuanced decision-making.
We use the arrow <-
more often for assignments in R.↩︎