Time for action – Using the continue keyword

Let's say that we want to display the number from 0 to 9 included, but not the number 8. We can use the continue keyword to do so, by directly going to the next iteration of our loop.

Let's write the following code that contains our loop, and a test done at each iteration to jump to the next iteration if needed:

for(i in 0...10)
{
   if(i==8)
   {
      continue;
   }
   trace(i);
}

What just happened?

In this Time for Action, we test if i is equal to 8, if it is, we simply go to the next iteration, therefore avoiding the printing of i.

This code will display the following:

TesthaXe.hx:12: 0

TesthaXe.hx:12: 1

TesthaXe.hx:12: 2

TesthaXe.hx:12: 3

TesthaXe.hx:12: 4

TesthaXe.hx:12: 5

TesthaXe.hx:12: 6

TesthaXe.hx:12: ...

Get haXe 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.