这里的 goto 语句(来自例如 c++)的 r 等价物是什么?

What would be an r equivalent of goto statements (from e.g. c++) here?

我意识到 r 没有 goto 语句,我目前正在翻译一些有它们的 C++ 代码。在下面的代码中,goto 循环应该使函数返回到“循环:”并从那里开始 运行 代码。您如何在 r 中获得类似的结果?

Solve <- function() 
{
  statements
  loop:
    iter_time <- iter_time + 1
  if (conditions) {
    statements
  } else if (conditions) {
    statements
    if(conditions) {
      statements
      if (conditions) {
        statements
      } else {
        statements
      }
    } else {
      goto loop
    }
  }
}

您可以使用 next 返回到循环的开头。例如:

for (i in 1:5) {
        if (i == 2) {
                print(paste0(i, " is a cool number!"))
        }
        else if (i == 3) {
                print(paste0(i, " is an awesome number!"))
        }
        else {
                next
        }
}