continue

continue , a built-in Tcl command, skips to the next iteration of a loop.

see also

break
breakeval
return

loop commands

for
foreach
while

documentation

official reference

synopsis

continue

description

continue, which is equivalent to return -level 0 -code continue , causes commands such as for, foreach, and while to abort evaluation of the current script and continue with the next iteration. Custom control structures are typically also designed to respond to continue in a similar fashion.

examples

Produce 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"
}

discussion

les: Given the following code:

foreach i $list1  {
    foreach j $list2  {
        if {[some condition]} continue
        do something
    }
}

continue begins the next iteration of the foreach j routine, but how does one begin the next iteration of the foreach i routine?

Place foreach i into a procedure 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 2005-04-20: In php, continue (and break) accept an argument that tells it how many nesting levels to continue/break for, slightly-similar to return -code. 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 routine.

les: These suggestions all involve creating yet another procedure 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
        }
        do something
    }
    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?).


PYK: another kind of sugar:

foreach i $list1 {
    set continue list
    foreach j $list2  {
        if  {[some condition]} {
            set continue continue
            break
        }
        do something
    }
    $continue
    ...
}

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 write up 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 write a tip for it, so i can't really complain that someone else hasn't either.

escargo 2005-04-20: 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 at [L1 ].