R:中断循环 [英] R: Break for loop

查看:58
本文介绍了R:中断循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能确认下一个break是否取消了内部for循环吗?

Can you confirm if the next break cancels the inner for loop?

   for (out in 1:n_old){

     id_velho <- old_table_df$id[out]
      for (in in 1:n)
      {
       id_novo <- new_table_df$ID[in]
       if(id_velho==id_novo)
       {
        break
       }else 
       if(in == n)
       {
       sold_df <- rbind(sold_df,old_table_df[out,])
       }
      }
    }

推荐答案

好吧,您的代码不可重现,所以我们永远不会确定,但这就是 help('break') 所说的:

Well, your code is not reproducible so we will never know for sure, but this is what help('break')says:

break 从 for、while 或重复循环;控制权转移到外面的第一个语句最内层循环.

break breaks out of a for, while or repeat loop; control is transferred to the first statement outside the inner-most loop.

所以是的,break 只会中断当前循环.您还可以使用例如:

So yes, break only breaks the current loop. You can also see it in action with e.g.:

for (i in 1:10)
{
    for (j in 1:10)
    {
        for (k in 1:10)
        {
            cat(i," ",j," ",k,"
")
            if (k ==5) break
        }   
    }
}

这篇关于R:中断循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆