Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to jump to next top level loop?

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 iteration of the outer loop?

uuu <- 0

for (i in 1:100) {
    uuu <- uuu + 1
    j <- 1000
    for (eee in 1:30) {
        j <- j - 1
        if (j < 990) {
            # if j is smaller than 990 I hope start next time of i
        }
    }
}
like image 268
user1421972 Avatar asked May 28 '12 15:05

user1421972


People also ask

How do you continue the outer loop?

When continue statement is used in a nested loop, it only skips the current execution of the inner loop. Java continue statement can be used with label to skip the current iteration of the outer loop too.

How do you jump from inner loop to outer loop?

To break out and not continue the loop3 use break; if (condition1_3) break; The break; statement only breaks out of the current loop i think, not the loop it is nested in.

When to use continue?

The continue statement is used when you want to skip the remaining portion of the loop, and return to the top of the loop and continue a new iteration. As with the break statement, the continue statement is commonly used with a conditional if statement.

How do you jump to the next loop in C++?

The goto statement can easily jump to the next outer loop cycle, no matter how many inner loops we got. A more complex alternative is to stop each of the inner loops with the break statement. And then jump over remaining code in the outer loop with the continue statement.

What is the use of jump statement in C++?

In C++ there is four jump statement: continue, break, return, and goto. Continue: It is used to execute other parts of the loop while skipping some parts declared inside the condition, rather than terminating the loop, it continues to execute the next iteration of the same loop.

How do you skip a line in a while loop?

Skip blank lines and comments using a continue statement. continue skips the remaining instructions in the while loop and begins the next iteration. The continue statement skips the rest of the instructions in a for or while loop and begins the next iteration. To exit the loop completely, use a break statement.

How to use the next function in a for-loop?

For this task, we can use the next function as shown below: for( i in 1:10) { # for-loop containing next function if( i % in % c (2, 5, 8)) next cat ( paste ("Iteration", i, "was finished. ")) } # Iteration 1 was finished. # Iteration 3 was finished.


1 Answers

@flodel has the correct answer for this, which is to use break rather than next. Unfortunately, the example in that answer would give the same result whichever control flow construct was used.

I'm adding the following example just to make clear how the behavior of the two constructs differs.

## Using `break`
for (i in 1:3) {
   for (j in 3:1) {     ## j is iterated in descending order
      if ((i+j) > 4) {
         break          ## << Only line that differs
      } else {
         cat(sprintf("i=%d, j=%d\n", i, j))
      }}}
# i=1, j=3
# i=1, j=2
# i=1, j=1

## Using `next`
for (i in 1:3) {
   for (j in 3:1) {     ## j is iterated in descending order
      if ((i+j) > 4) {
         next           ## << Only line that differs
      } else {
         cat(sprintf("i=%d, j=%d\n", i, j))
      }}}
# i=1, j=3
# i=1, j=2
# i=1, j=1
# i=2, j=2   ##  << Here is where the results differ
# i=2, j=1   ##
# i=3, j=1   ##
like image 51
Josh O'Brien Avatar answered Sep 18 '22 07:09

Josh O'Brien