

Task

Demonstrate a   for-loop   where the step-value is greater than one.



Related tasks

   Loop over multiple arrays simultaneously
   Loops/Break
   Loops/Continue
   Loops/Do-while
   Loops/Downward for
   Loops/For
   Loops/For with a specified step
   Loops/Foreach
   Loops/Increment loop index within loop body
   Loops/Infinite
   Loops/N plus one half
   Loops/Nested
   Loops/While
   Loops/with multiple ranges
   Loops/Wrong ranges


class Step {
  var end:Int;
  var step:Int;
  var index:Int;
 
  public inline function new(start:Int, end:Int, step:Int) {
    this.index = start;
    this.end = end;
    this.step = step;
  }
 
  public inline function hasNext() return step > 0 ? end >= index : index >= end;
  public inline function next() return (index += step) - step;
}
 
class Main {
  static function main() {
    for (i in new Step(2, 8, 2)) {
      Sys.print('$i ');
    }
    Sys.println('WHOM do we appreciate? GRAMMAR! GRAMMAR! GRAMMAR!');
  }
}