Continue and Break
We briefly saw break in Chapter II where it was used to exit an infinite while true loop. Let’s look at break and its counterpart continue in more detail.
Break
The break command immediately stops the current loop and moves execution to the first command after the loop:
int ifor i = 1 to 100 if i == 6 then break end print_formatted_now "i = %d" 500 i wait 500end
// execution continues here after breakprint_string_now "Loop stopped" 2000This displays values 1 through 5. When i becomes 6, the break command exits the loop and the script displays "Loop stopped". It works the same way in while and for loops.
Continue
The continue command skips the rest of the current iteration and moves on to the next one:
int ifor i = 1 to 5 if i == 3 then continue end print_formatted_now "i = %d" 500 i wait 500endThis displays 1, 2, 4, 5. The value 3 is skipped because continue jumps to the next iteration before print_formatted_now is reached.
Practical Example
Here is a more practical use of break. This script gives the player money every second, but stops early if the F6 key is pressed:
const KEY_F6 = 117
int ifor i = 1 to 10 if is_key_pressed KEY_F6 then break end add_score 0 100 print_formatted_now "+$100 (round %d of 10)" 1000 i wait 1000endWithout break, the only way to achieve this would be a while loop with a more complex condition. break gives you a clean way to exit early when something unexpected happens.