cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
jan_solo
Level III

Break/Continue a (nested) outer for loop

If I have 2 nested for-loops, how can I break/continue the outer loop?

example:

for (i=0, i<10, i++,

     for (j=0, j<10, j++,

          // Do stuff

         if ( <condition>,

               // Continue with the next i

          ); // end if

    ); // end inner loop

    

     // Do some more stuff

); // end outer loop

1 ACCEPTED SOLUTION

Accepted Solutions
ron_horne
Super User (Alumni)

Re: Break/Continue a (nested) outer for loop

hi Jan,

try to use the Break(). the following script will stop running when i will reach 3.

For( i = 0, i <= 5, i++,

    For( z = 0, z < 5, z++,

    if (i >= 3, Break());

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    )

);

the next script will just skip i=3 and continue to i=4 and i=5.

For( i = 0, i <= 5, i++,

    For( z = 0, z < 5, z++,

    if (i== 3, Break());

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    )

);

please take a look at this link for the explanation about the use Break and while:

Iterate

ron

View solution in original post

3 REPLIES 3
ron_horne
Super User (Alumni)

Re: Break/Continue a (nested) outer for loop

hi Jan,

try to use the Break(). the following script will stop running when i will reach 3.

For( i = 0, i <= 5, i++,

    For( z = 0, z < 5, z++,

    if (i >= 3, Break());

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    )

);

the next script will just skip i=3 and continue to i=4 and i=5.

For( i = 0, i <= 5, i++,

    For( z = 0, z < 5, z++,

    if (i== 3, Break());

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    )

);

please take a look at this link for the explanation about the use Break and while:

Iterate

ron

jan_solo
Level III

Re: Break/Continue a (nested) outer for loop

Thank you for the answer.

It's not completely what I am looking for.

Consider this:

For( i = 0, i <= 5, i++,

    For( z = 0, z < 5, z++,

    if (i== 3, Break());

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    );

  

    Print ( "OUTER i = " || Char (i));

  

);

If I run this script, outer i is printed.  The real problem here is to 'continue with the next i', without the second print-line (OUTER i = ...) will be printed.  Is there an easy way to do, or do I just have to use a boolean 'continueOuterLoop'?

Thanx.

Boolean solution:

For( i = 0, i <= 5, i++,

   

    continueOuterLoop = 0;

   

    For( z = 0, z < 5, z++,

    if (i== 3, continueOuterLoop = 1; Break(); );

       

        Print( "i=" || Char( i ) || " & z=" || Char( z ) )

    );

   

   

    if (continueOuterLoop == 0,

        Print ( "OUTER i = " || Char (i))

        )

   

);

jwiltsie
Level III

Re: Break/Continue a (nested) outer for loop

Hi Jan, Couldn't you just add an i++ instead of Break() during the inner For loop? 

You could also set z=0 after i++, if you wanted it to restart the inner For loop without going into the outer For loop.

 

For example:

 

For( i = 0, i <= 5, i++,
    For( z = 0, z < 5, z++,
		if (i== 3,
			Print( "i=" || Char( i ) || " & z=" || Char( z ) );
			i++; // I put the i++ after the print so that it prints i=3
		);
	);
    Print ( "OUTER i = " || Char (i));
);