jashkenas/coffee-script — Issue #2047: Range loop condition shouldn't ignore step value
Author: dstepanov
Created: 2012-01-16 21:50:52+00:00
State: open

When using **for** with a range and a step value CS is determining the iteration condition by its sign in a case when value is available, otherwise it will compare min and max values of the range definition.

I don't think it is the correct behaviour I would expect it to use the step value to determine its condition.

Example:

```coffeescript
for i in [@min..@max] by 1
alert i
```
```javascript
var i, _ref, _ref2;
for (i = _ref = this.min, _ref2 = this.max; i <= _ref2; i += 1) {
alert(i);
}
```

Negative step value with change the condition:

```coffeescript
for i in [@min..@max] by -1
alert i
```
```javascript
var i, _ref, _ref2;
for (i = _ref = this.min, _ref2 = this.max; i >= _ref2; i += -1) {
alert(i);
}
```

Unknown step value will not effect the condition of the loop in runtime:

```coffeescript
for i in [@min..@max] by @steps
alert i
```
```javascript
var i, _ref, _ref2, _ref3;
for (i = _ref = this.min, _ref2 = this.max, _ref3 = this.steps; _ref <= _ref2 ? i <= _ref2 : i >= _ref2; i += _ref3) {
alert(i);
}
```

In my case I can't see an additional **if** to be useful, when min > max and a step value is positive it will cause an infinite loop.