For expression

On the previous wiki page, we looked at the while and do-while expressions to create program loops. The third way to loop is using the for-expression.

Go to and look under the For heading. Its syntax is:

for( counter-variable in lower-limit...upper-limit ) { expr-loop };

Lets print out the numbers from 1 to 10 using a "for loop" and print out their sum (the total when they are added together) in Haxe.

class Forloop
  public static function main()
    var sum = 0;
    for( i in 1...11 )
      Sys.println( i );
      sum += i;
    Sys.println( "The sum of the numbers is " + sum );

The output of the program is:
The sum of the numbers is 55

What Happened?

  1. We initialise a variable named sum and assign it the value of 0 in line 3. Note that we do this before we need to change its value inside the loop. If we do this inside the loop, it will be assigned the value of 0 each time through the loop and will end up with the final value of the counter-variable.
  2. We set up the for-cond in line 4. The i variable will be the current value of the counter-variable. The initial value assigned to i is the lower-limit. The last value assigned to i is the upper-limit. The value of i is incremented by 1 each time through the loop. It is easy to see the limits of a for-loop because all its information is in one line.
  3. The current value of i is printed on screen followed by a newline character in line 5.
  4. The current value of the sum variable is incremented by i in line 6. See Operators under the heading of Assignment Operator for details on how this works.
  5. We have reached the end of expr-loop. Execution returns to for-cond and it is evaluated again. While it is true, the expr-loop is repeated.
  6. When i is 11, for-cond is not true (false), so execution continues at line 8 and the final message is printed on screen.

More Facts

  • For loops are different from traditional C loops, in that they can only be used with iterators. See Use a while or do-while loop if you wish to increment by other than 1.
  • We can use the counter-variable (i in this case) inside the loop, but we should not change its value there.
  • If the iterator was created with the ... operator, with its 2nd term smaller than or equal to the 1st, then the loop will not execute. If the ... operator is used as above (inline, inside the for), then a smaller second term will give a compiler error, explaining the problem. See under the For heading.
  • You can create for-loops like "for( i in -2...3 ) { Sys.println( i ); }"
  • Use the Sys.println() syntax because neko.Lib.println() will not be available in Haxe 3.

For You To Try Part I

Lets use the scores array from Array with a for-loop and do an average calculation. To recap, the array was created with its values as follows:

var scores : Array<Int> = [110,170,870,70,350,240,330];

To find the average of a set of numbers, find their sum and divide this by the count of the numbers.

We will use an Array attribute length (see your local Haxe API under Array or to count the number of items in the array.

Pseudocode for class Average

create the array scores with its values
initialise variable sum to 0
set up the for loop: i variable, 0...scores.length
 assign to total after incrementing by current score
 // sum += scores[i]
print "The average is " and sum / scores.length

Note that we should use Math.ceil() (see to round up to the nearest integer value for our average calculation. (306)

For You To Try Part II

Here is a set of reading test scores for a group of students. What is the average reading score of this group?


Also, think of and implement a method for finding out the lowest reading score from the set. Do the same to find the highest reading score from the set. If you need a hint, initialise a variable and then compare it to each value in the set. Only if it is greater than / less than the variable, change the value of the variable to the new value.

Where To From Here?

In this flow-of-control section, you have seen so far how to use if-expressions, make while-loops and do-while loops and now for-loops. The next topic to look at is the switch - case - default expression.