How to jump to next top level loop?

后端 未结 2 1170
滥情空心
滥情空心 2021-02-19 02:13

I have a for loop nested in another for loop. How can I make it so that when somethign happens in the inner loop, we exit and jump to the next iteratio

2条回答
  •  野性不改
    2021-02-19 02:46

    I think you want to use break so R will stop looping through your inner for loop, hence proceed to the next iteration of your outer for loop:

    for (i in 1:10) {
       for (j in 1:10) {
          if ((i+j) > 5) {
             # stop looping over j
             break
          } else {
             # do something
             cat(sprintf("i=%d, j=%d\n", i, j))
          }
       }
    }
    # i=1, j=1
    # i=1, j=2
    # i=1, j=3
    # i=1, j=4
    # i=2, j=1
    # i=2, j=2
    # i=2, j=3
    # i=3, j=1
    # i=3, j=2
    # i=4, j=1
    

提交回复
热议问题