Loop

A loop in programming is a code block that executes any number of times - including not at all, or only once, but usually many more than 1. The following is a list of loops in Game Maker.

do...until
The do-until loop repeats the until the is true. Each times it performs the, it then evaluates the , if it is true, it stops and moves to the next action, if it is false, the loop is repeated, then the statement is re-evaluated, and so on.

Example
i = 0; do{ show_message("Count: " + string(i)); i += 1; } until (i == 5);

while
The while loop repeats the as long as the is true. Once the becomes false, the loop is stopped.

Example
test = false; i = 0; while(!test){ show_message(i); if(i == 5){ test = false; }    i += 1; }

repeat
The repeat loop repeats the a constant number of times defined when the repeat loop is called.

Example
i = 0; repeat(5){ // Repeats the loop 5 times show_message("Count: " + string(i)); i += 1; }

for
The for loop executes a statement, evaluates an expression, and if the expression is true executes a statement block and then the second statement is executed before evaluating the expression again.

Example
for(i = 0; i < 10; i += 1){ show_message("Number: " + string(i)); }

Efficiency of Loops
Many users may wonder which loop is best to use. The following test was done in GM8 on Windows Vista running in Safe Mode, to prevent errorous results. For each loop type, a variable was created, then 1 was added to it 5000 times with a room speed of 500. The single object was placed in an otherwise blank, default room. The repeat loop bounced around in the 350-400 fps range. The for loop ran at about 150 fps. The while loop was 225-250 fps. The do loop has 250fps.