Why is this function correct?

is_leap_year <- function(year) {
  # If year is div. by 400 return TRUE
  if(is_divisible_by(year, 400)) {
    return(TRUE)
  }
  # If year is div. by 100 return FALSE
  if(is_divisible_by(year, 100)) {
    return(FALSE)
  }  
  # If year is div. by 4 return TRUE
  if(is_divisible_by(year, 4)) {
    return(TRUE)
  }
  # Otherwise return FALSE
  FALSE
}

Notice that here if year is div. by 100, then return FALSE. So my understanding is if I input year==2000, then I just get FALSE. However, it gives me correct TRUE. How come?

Because in this case the first condition is met, the return statement will be executed and therefore further conditions will not be checked.

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.