Flow-control structures: Difference between revisions

Content added Content deleted
(Asynchronous transfer of control added)
Line 738: Line 738:


=={{header|Tcl}}==
=={{header|Tcl}}==

=== after ===

The <tt>after</tt> facility can be used to execute some code at some future time asynchronously, like this

after 1000 {myroutine x}

which will call "<tt>myroutine</tt>" with parameter "<tt>x</tt>" 1000ms from 'now'; no matter what other code might be running at the time (i.e. "<tt>after</tt>" schedules the execution, then returns and continues program flow with the following code).

The scheduled task can be removed from the scheduler for example with

after cancel myroutine

(other ways are possible).

The correct way to schedule some regularly recurring task in TCL is to incorporate a self-scheduling at the end of the routine. For example the following will produce a clock whose display is updated once a second:

proc update {} {
.clockface configure -text [clock format [clock seconds]]
after 1000 update ; # call yourself in a second
}
# now just create the 'clockface' and call ;update' once:
pack [label .clockface]
update

=== loop control ===
=== loop control ===
Tcl has the <code>break</code> command to abort the current loop (<tt>for</tt>/<tt>foreach</tt>/<tt>while</tt>) and the <code>continue</code> command to skip to the next loop iteration.
Tcl has the <code>break</code> command to abort the current loop (<tt>for</tt>/<tt>foreach</tt>/<tt>while</tt>) and the <code>continue</code> command to skip to the next loop iteration.