Complex conditions

Control flow / Conditional statements / if/else statements

first <- 3
second <- 5
third <- 7

if (third >= first && third >= second) {
  cat("nothing is larger than third\n")
}
# nothing is larger than third

if (!(first >= second || first >= third)) {
  cat("first is the smallest\n")
}
# first is the smallest

# No chained comparison: write both parts
if (first < second && second < third) {
  cat("second is between first and third\n")
}
# second is between first and third

# Trap: a chain is a syntax error
chain_error <- tryCatch(
  parse(text = "1 < 2 < 3"),
  error = conditionMessage)
cat("chain_error =", chain_error, "\n")
# chain_error = <text>:1:7: unexpected '<'
# 1: 1 < 2 <
#           ^