Version 20 of continue

Updated 2012-11-30 19:44:03 by pooryorick

COMMAND

continue - Skip to the next iteration of a loop

USAGE

continue

CONTEXT

TCL core command

DESCRIPTION

This command is typically invoked inside the body of a looping command such as for or foreach or while. It returns a TCL_CONTINUE code, which causes a continue exception to occur. The exception causes the current script to be aborted out to the innermost containing loop command, which then continues with the next iteration of the loop. Catch exceptions are also handled in a few other situations, such as the catch command and the outermost scripts of procedure bodies.

MAN PAGE

http://www.tcl.tk/man/tcl8.5/TclCmd/continue.htm

SEE ALSO

break, return

loop commands: for, foreach, while

EXAMPLES

Print a line for each of the integers from 0 to 10 except 5:

 for {set x 0} {$x<10} {incr x} {
   if {$x == 5} {
      continue
   }
   puts "x is $x"
 }

LES: Suppose the following code:

 foreach  i  $List1  { 
 
     foreach  j  $List2  { 

        if  [some condition] {continue}

        eval {blah blah blah}
     }
 }

Continue interrupts the foreach j loop. But what if I wanted it to break the foreach i loop?

Place the [foreach i] command in a proc and use return instead of continue.

Lars H: Or use breakeval as follows

  foreach i $List1 { 
     breakeval-aware foreach j $List2 { 
        
        if  {[some condition]} then {
           breakeval {continue} ; # Break out of j loop, do a continue for the i loop.
        }

        eval {blah blah blah}
     }
 }

MG April 20th 2005 - In PHP, continue (and break) accept an argument that tells it how many nesting levels to continue/break for, slightly-similar to return's -code option. How hard would it be to add something like that into Tcl? Since neither break nor continue accept any arguments right now, as long as it defaulted to 1 it would have total backwards-compatability, and would, IMHO, be more "natural" than the above.

schlenk Did you try return -code continue -level ... Lars H: That won't work, because it wouldn't become a continue until it returns from some proc.

LES: These suggestions all involve creating yet another proc for my code, which is not really what I was looking for. I was indeed thinking of PHP's continue/break argument, as MG mentioned. I asked the question in the first place because I already have a solution, but it forces me to add more code to an already very long proc that I am trying to do some liposuction on. Some sort of "loop depth argument" would help me make it shorter. Anyway, here is my current solution:

 foreach  i  $List1  { 
 
     set  _break  0
     foreach  j  $List2  { 

        if  [some condition] {incr _break;  break }

        eval {blah blah blah}
     }

     if { $_break > 0 }  { continue }
 }

Conclusion: if you come from PHP, now you know that there is no continue/break "loop depth argument". But several workarounds are offered above (and below?).

LV Given that there's already the weird return arguments, what is the argument against enhancing continue as described above? Is there a technical issue with the proposal?

RHS The idea of enhancing break and continue to accept an optional depth argument has been brought up multiple times in the past. I've never seen a counter-argument as to why it would be a bad idea... I've never seen anyone with enough motivation to even writeup a TIP for it either, though. I've had cases where I'd like to have had the feature, and I'm too lazy to writeup a TIP for it, so I can't really complain that someone else hasn't either.

escargo 20 Apr 2005 - In Icon break and next (the equivalent of continue) both have optional expressions that are evaluated in the context outside the current loop. So, if you are in inner loop and you want to leave that one loop, you use break. If you want to leave both an inner and outer loop, you would use break break.

Lars H: There is such an RFE logged for Tcl [L1 ].