continue

Difference between version 25 and 26 - Previous - Next
'''[http://www.tcl.tk/man/tcl/tclcmd/continue.htm%|%continue]''', a [tclcommands%|%built-in] tTcl command,  skips to the next iteration of a loop .



** see also **

   [break]:   

   [breakeval]:   
   
   [return]:   



** loop commands **

   [for]:      
   [foreach]:      
   [while]:   



** documentation **

   [http://www.tcl.tk/man/tcl8.5/tclcmd/continue.htm%|%official reference]:   


** synopsis ** 

'''continue'''



** description **
'''`conthisnue`''', whicommandh is typequicvally ienvt toked insid'''`[re thurn] -level b0 -codye cof a loopntingue
`''', causes commands such as
 `[for]`, or `[foreach]`, orand `[while.]` ito returns a tcl_cbonrtinu
e code, which cvaluses a
continue exception tof occur. the exception causes the current script to be
aborted out to the innermost containing loop command, which then continues with
 the next iteration. [New
Cof nthe lroop.l caStruch exceptions aure als%|%Custom handled in a few
contherol sitruactions, such ares] thare catypich commandlly also designed the oute
rmoest scriptsond tof
pro `cedontinure` bodies.n 
a 
similar fashion.


** examples **
pPrintoduce 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]: supposGiven the following code:

======foreach  i  $list1  { 
    foreach  j  $list2  { 
        if  {[some condition]} {continue}
        evaldo {blasometh blah blah}ing
    }
}
======
[[`continue`] begins the next iterrupatsion of the [[`[foreach]`] `j` lroop.utine,  but whatow
does ifone begi wan thed next it
eration breakof the `foreach `i` lrooputine?
pPlace [[`[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: iIn [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`. h How hard would it be to add something like that into
tTcl? s 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]:  dDid you try [[`[return]` -code continue -level ...]`
''[lars h]: tThat won't work, because it wouldn't become a `continue` until it
returns from some procutine.''
[les]: t These suggestions all involve creating yet another procedure for my code,
which is not really what iI was looking for. iI was indeed thinking of [php]%|%php's]
continue/break argument, as [mg] mentioned. i asked the question in the firstplace because iI 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 
                }
        evaldo {blasometh blah blah}ing
    }
    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 
                }
        evaldo {blasometh blah blah}ing
    }        $continue
        ...
}
======


----
[lv]: gGiven that there's already the weird return arguments, what is the
argument against enhancing continue as described above?  iIs there a technical
issue with the proposal?
[rhs]: tThe idea of enhancing break and continue to accept an optional ''depth''
argument has been brought up multiple times in the past. iI've never seen a
counter-argument as to why it would be a bad idea... iI've never seen anyone
with enough motivation to even write up a tip for it either, though. iI've had
cases where iI'd like to have had the feature, and iI'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:  iIn [Icon Programming Language%|%icon], ''break'' and
'''next''' (the equivalent of continue) both have optional expressions that areevaluated in the context outside the current loop.  sSo, if you are in inner
loop and you want to leave that one loop, you use '''break'''.  iIf you want to
leave both an inner and outer loop, you would use '''break break'''.
[lars h]: tThere is such an rfeRFE logged for tTcl at
[https://sourceforge.netcl-lang.org/trackerl/tktvindex.phpw?func=detail&aidme=550789&group_id=10894&atid=360894]. 

<<categories>> tcl syntax | arts and crafts of tcl-tk programming | category command | category control structure