Condition
day <- "sunday"

if (day %in% c("sunday", "saturday")) {
  print("Rest!")
} else if (day == "monday") {
  print("Groan!")
} else {
  print("Work!")
}

Using tryCatch for error handling:

result <- tryCatch({
  10 / 0
}, warning = function(w) {
  "Warning occurred!"
}, error = function(e) {
  "Error occurred!"
}, finally = {
  print("Cleanup complete.")
})

print(result)
Loops

For loop example:

for (i in 1:5) {
  print(paste("Iteration", i))
}

While loop example:

count <- 0
while (count < 5) {
  print(paste("Count is", count))
  count <- count + 1
}