Альтернативная форма

Поток управления / Условные операторы / Операторы if/else

latitude <- sample(-90:90, 1)

# if is an expression: it gives the value of
# the branch that ran
location <- if (latitude == 0) {
  "Equator"
} else if (latitude < 0) {
  "Southern Hemisphere"
} else {
  "Northern Hemisphere"
}

# On one line
sign <- if (latitude < 0) "-" else "+"

# Trap: no else and a false condition give NULL
no_else <- if (latitude > 90) "too far"

cat("latitude =", latitude, "\n")
cat("location =", location, "\n")
cat("sign =", sign, "\n")
cat("is.null(no_else) =", is.null(no_else), "\n")
# is.null(no_else) = TRUE