The "Break" Statement

This section explains the various uses for the "break" statement.

The "break" is used to end prematurely a for, repeat, while, do... until loop of some kind, or to tell a switch statement to end at that point, or to prematurely end a with function. Below you can see a few examples of how this can be used, and its syntax is simply:

break;



"break" in a for loop:

{
var i;
for (i = 0; i < 10; i += 1)
   {
   if array[i] = 234 break;
   }
num = i;
}


"break" in a repeat loop:

{
var i, temp;
i = 0;
temp = 0;
repeat (10)
   {
   temp += array[i];
   if temp > max_total break else i += 1;
   }
}


"break" in a while loop:

{
var i;
i = 0;
while (!place_free(x, y))
   {
   x = random(room_width);
   y = random(room_height);
   if i > 50 break else i+=1;
   }
}


"break" when using with:

{
var count = 0;
with (obj_Enemy)
   {
   count++;
   if count > 10 break;
   hp = 100;
   }
}


Back: GML Overview
Next: The "Continue" Statement
© Copyright YoYo Games Ltd. 2018 All Rights Reserved