Book Image

haXe 2 Beginner's Guide

5 (1)
Book Image

haXe 2 Beginner's Guide

5 (1)

Overview of this book

Table of Contents (21 chapters)
haxe 2
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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