54

In CoffeeScript, the while loop comes standard:

while x()
   y()

However, the following1 doesn't work:

do
  y()
while x()

And this is simply sugar for the first example:

y() while x()

Does CoffeeScript come with a built-in loop that executes at least once?

1As an aside, do is a keyword — it's used to call anonymous functions.

ClosureCowboy
  • 20,825
  • 13
  • 57
  • 71
  • 8
    OT, but I loled at the description of the `coffeescript` tag. :) – Mateen Ulhaq May 19 '11 at 01:54
  • one should ask oneself whether a given construct is very desirable at all. the important distinction between `while` and `do...while` is that the loop body is executed once, as the condition is only checked after the loop body has executed. in CS you can easily do that using a `do...break if...` or `do...break unless...` construct which reads just as cleanly & *is more flexible* since the `break` can occur on any line in the loop body. this also applies to similar constructs like pascal's `repeat...until`. – flow Jun 20 '13 at 13:35
  • @flow `do...break if...` incurs IIFE penalty – Pete Alvin Jul 21 '15 at 16:56

5 Answers5

68

The CoffeeScript documentation says:

The only low-level loop that CoffeeScript provides is the while loop.

I don't know of a built-in loop that executes at least once, so I guess the alternative is

loop
  y()
  break if x()
Alex Korban
  • 14,916
  • 5
  • 44
  • 55
24

I know that this answer is very old, but since I entered here via Google, I thought someone else might as well.

To construct a do...while loop equivalent in CoffeeScript I think that this syntax emulates it the best and easiest and is very readable:

while true
   # actions here
   break unless # conditions here
Bertie Wheen
  • 1,215
  • 1
  • 13
  • 20
adrenalin
  • 1,656
  • 1
  • 15
  • 25
  • 6
    `while true` can be written as `loop` (and is more descriptive: you're constructing a loop, and it doesn't continue as long as `true`) – 1j01 Jan 27 '15 at 04:14
  • 2
    @1j01 Retrospectively I agree that `loop` is semantically better after nearly two years, but this is an alternative and after writing my JS with CoffeeScript for a few years it is still my preferred method, probably just out of a habit. – adrenalin Jan 27 '15 at 11:03
16

Your guess is correct: There is no do-while equivalent in CoffeeScript. So you'd typically write

y()
y() while x()

If you find yourself doing this often, you might define a helper function:

doWhile = (func, condition) ->
  func()
  func() while condition()
Trevor Burnham
  • 76,828
  • 33
  • 160
  • 196
3

I found this could be accomplished through a short circuit conditional:

flag = y() while not flag? or x()
Sukima
  • 9,965
  • 3
  • 46
  • 60
0

I've been working on a project where I simply force the condition to evaluate at the end of the loop, then terminate at the beginning.

# set the 'do' variable to pass the first time
do = true
while do

  # run your intended code
  x()

  # evaluate condition at the end of
  # the while code block
  do = condition

# continue code

It's not very elegant, but it does keep you from defining a new function just for your while code block and running it twice. There generally is a way to code around the do...while statements, but for those time that you can't you have a simple solution.

benja2729
  • 95
  • 1
  • 2