Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Were `do...while` loops left out of CoffeeScript...?

Tags:

coffeescript

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.

like image 299
ClosureCowboy Avatar asked May 19 '11 01:05

ClosureCowboy


People also ask

Why are my while loops not working?

The while loop is not run because the condition is not met. After the running the for loop the value of variable i is 5, which is greater than three. To fix this you should reassign the value before running the while loop (simply add var i=1; between the for loop and the while loop).

How do I know if CoffeeScript is installed?

The coffee and cake commands will first look in the current folder to see if CoffeeScript is installed locally, and use that version if so. This allows different versions of CoffeeScript to be installed globally and locally.

Why does while loop not stop?

The issue with your while loop not closing is because you have an embedded for loop in your code. What happens, is your code will enter the while loop, because while(test) will result in true . Then, your code will enter the for loop. Inside of your for loop, you have the code looping from 1-10.

How often does while loop check?

A while loop evaluates its condition before the first iteration, and inbetween each subsequent iteration. The condition is never evaluated inside the loop body.


1 Answers

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() 
like image 94
Alex Korban Avatar answered Sep 30 '22 21:09

Alex Korban