FOR Loop
In Chapter II, we learned about the while loop which repeats as long as a condition is true. But what if you know exactly how many times you want to repeat something? The for loop is designed for exactly that.
Syntax
for <variable> = <start> to <end> <body>endThe loop variable starts at <start> and increases by 1 after each iteration until it passes <end>.
For example, to add money to the player 5 times:
int ifor i = 1 to 5 add_score 0 100 wait 250endThe variable i will take values 1, 2, 3, 4, 5, and the loop body will execute 5 times. After the loop, the player has $500 more.
Counting Backwards with DOWNTO
To count in the opposite direction, use downto instead of to. The loop variable decreases by 1 after each iteration:
int ifor i = 5 downto 1 print_formatted_now "Countdown: %d" 1000 i wait 1000end
print_string_now "Go!" 2000This displays 5, 4, 3, 2, 1 and then Go!.
Custom Step
By default, the loop variable changes by 1 on each iteration. You can change this with the step keyword:
int ifor i = 0 to 100 step 10 print_formatted_now "Value: %d" 500 i wait 500endThis displays 0, 10, 20, 30, …, 100.
step works with downto as well:
int ifor i = 100 downto 0 step 25 print_formatted_now "Value: %d" 500 i wait 500endUsing Constants with FOR
Constants pair nicely with for loops to make the code easy to configure:
const MAX_ROUNDS = 10const REWARD = 50
int roundfor round = 1 to MAX_ROUNDS add_score 0 REWARD wait 500endChanging MAX_ROUNDS or REWARD at the top of the script instantly adjusts the loop behavior.