See also while...wend in Tcl
There is a difference between do..until and while..wend. By do..until the statment is executed at least once
We can simulate such behaviour in Tcl by:
while 1 { # ..statement if {!expresion} break }
DKF: Try this:
proc do {body keyword expression} { if {$keyword eq "while"} { set expression "!($expression)" } elseif {$keyword ne "until"} { return -code error "unknown keyword \"$keyword\": must be until or while" } set condition [list expr $expression] while 1 { uplevel 1 $body if {[uplevel 1 $condition]} { break } } return }
fredderic: How about this version:
proc do {command while test} { if { $while eq "until" } { set test "!($test)" } elseif { $while ne "while" } { error "unknown keyword \"$while\": must be while or until" } set code [catch {uplevel 1 $command} result] switch -exact $code { 0 - 4 {} 3 {return} default {return -code $code $result} } set code [catch {uplevel 1 [list while ${bool}($test) $command]} result] return -code $code $result }
It's a version I wrote quite some time ago, augmented with DKFs somewhat neater keyword check. The advantage is that this one runs the first pass manually, and then runs the rest of the loop directly in-place. I think the catch handling may need a little attention, and I personally like returning the result of the last body iteration.
wdb This is my one which allows to use while as well as self-defined constructions such as until:
proc ::do {code while cond} { # nicht-abweisende Schleife uplevel 1 $code uplevel 1 [list $while $cond $code] }