Time for action—Using the break keyword

The break keyword allows one to exit a loop prematurely. Now, let's imagine that we have a loop that has 10 iterations, but we only want to go through the first eight ones.

Let's write a simple loop from 0 to 9 included. Note that we want to exit this loop as soon as we hit the 9th one.

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

What just happened?

At the beginning of each loop, we test whether we are in the 9th one or not, if we are, then we exit the loop by executing break.

The result of this code will be:

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: 7

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.131.38.14