3
$\begingroup$

An until loop construct until (x) can almost always be achieved with a while loop while (!x). What's a possible reasoning behind having it available when a language also provides a while loop?

$\endgroup$
7
  • $\begingroup$ By "until loop" do you mean do-while loop? $\endgroup$
    – Ginger
    Commented May 19, 2023 at 19:28
  • $\begingroup$ @Ginger until(x) means while(!x). $\endgroup$ Commented May 19, 2023 at 19:31
  • 1
    $\begingroup$ I see it the same way I see Perl's unless. As in, just more filler. $\endgroup$ Commented May 19, 2023 at 19:33
  • 3
    $\begingroup$ Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. $\endgroup$ Commented May 19, 2023 at 19:56
  • 1
    $\begingroup$ @UnrelatedString And that’s ok! $\endgroup$ Commented May 24, 2023 at 8:29

5 Answers 5

8
$\begingroup$

Clarity in beginner languages

A construct like this could be helpful in a language designed for new programmers, like Scratch. "until x" may sound more natural than "while not x" to a new developer.

$\endgroup$
2
  • $\begingroup$ I admit my logic here is rather tenuous, but I can't think of any other good reason to have this construct. $\endgroup$
    – Ginger
    Commented May 19, 2023 at 19:35
  • 1
    $\begingroup$ Some languages have a unless keyword for the same purpose $\endgroup$
    – mousetail
    Commented May 19, 2023 at 20:22
6
$\begingroup$

while let and until let

In Swift, while let allows you to call a function repeatedly and loops until it returns nil:

// Simple cat program. readLine (builtin) reads a single line from stdin, and
// returns nil when there's no input left.
while let line = readLine() {
  // Because we made it here, we know line is not nil.
  print(line)
}
// readLine returned nil, so we're done.

But what if you want to loop until something returns non-nil?

var next: Task?
repeat {
  next = getNextTask()
} while next == nil
guard let next else { fatalError("unreachable") }

Blech, that's a mess. If Swift had until let, this code would be simpler:

until let task = getNextTask() {
  // task isn't usable here; even if it were, its value would always be nil.
}
// task is usable here, and known to not be nil.
$\endgroup$
3
  • $\begingroup$ This would function basically the same as rusts loop {if task = getNextTask(){break task} } construct $\endgroup$
    – mousetail
    Commented May 20, 2023 at 15:44
  • $\begingroup$ That's true, but Swift loops are not expressions, and you can't break with a value. $\endgroup$
    – Bbrk24
    Commented May 20, 2023 at 15:45
  • $\begingroup$ Your until let basically turns the loop into a expression with immediate assignment $\endgroup$
    – mousetail
    Commented May 20, 2023 at 15:45
5
$\begingroup$

In addition to Ginger's answer, I have 2 things to add:

  1. Avoiding some errors. They can prevent errors from a mistakenly negated condition in a while loop.
  2. Readability. It is more clear the programmers intention, making the code more readable.
$\endgroup$
2
$\begingroup$

Code Golf

In Code Golf, where most of us come from, you try to make your code as short as possible. until x is shorter than while not x and is also shorter than while !x.

$\endgroup$
1
$\begingroup$

Run the block at least once

In languages that use keywords to delimit blocks (rather than braces or indentation), e.g. If condEndif and While condEndWhile, a RepeatUntil cond provides a way to get the block run at least once. A language could even allow While condAUntil condB for full control. Putting an "until" after a block also feels quite natural, as in "do this until that happens". An example of a language that has this is APL, e.g.:

:While 100>i
:AndIf 100>j
    (i j)←foo i j
:Until 100<i+j
:OrIf i<0
:OrIf j<0
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .