Version 12 of until

Updated 2017-07-26 21:22:52 by LarrySmith

The procedure until is not in the core, but you can define it yourself as follows:

proc until {cond code} {
    uplevel 1 [list while !($cond) $code]
}

See also control::do (in tcllib).

PL: The original rewriting of the command was like this:

list while !$cond $code

but that doesn't work since a cond value of, say, $i > 5 is rewritten as the condition argument !$i > 5 which will always evaluate to false (the ! operator has priority, and the result of evaluating it is either 1 or 0, which is never greater than 5). Other condition strings are likely to lead to other interesting errors.

Rewriting the command as

list while !($cond) $code

ensures that all of $cond is evaluated before negating the result.


This doesn't implement the usual meaning of until, which executes the code and then tests cond after.

Larry Smith I use:

  # an unbounded loop or one with a terminating condition of while or until.
  # always executes at least once. If no while or until conditions is attached,
  # loops forever until <break>.
  proc repeat {args} {
    > script whenexit test
    if {[catch {^^ $script} err]} ^
    case $whenexit {
      ""       {^^ "while 1 \{$script\}"}
      "while"  {^^ "while \{$test\} \{$script\}"}
      "until"  {^^ "while \{!($test)\} \{$script\}"}
    }
  }

The advantage of the above is you can leave out the while or until entirely and just use break to exit from anywhere in the loop.

DKF: The usual definition is typically expressed as a do-while loop, which is a form that puts the test last. When expressed condition-first, it is natural to expect the condition to be checked first...

RLH Someone else was thinking about this too: until