The anatomy of a loop
An iterative loop always starts with the for
keyword. It has three important sections:
Where to start
Where to end
What to do after every loop finishes
In this case, we start by declaring an iterator variable called i
of type int
(integer), and set that iterator i
to zero.
var i:int = 0
Next, we say that we're going to loop as long as the value of i
is less than (<
) the value of rows. Because we've already set rows to 4
, this code will loop four times.
i<rows
In the third section, we increase the value of i
by one. Here's how the interpreter chews through our loop:
Set an integer variable called
i
to0
.Check to see if
i
is less thanrows
(4
). 0 is less than 4, so let's go!Run the code inside the loop.
When we're finished, increase
i
by one. (i++
).i
is now 1.Check again to see if
i
is less thanrows
(4
). 1 is less than 4, so let's keep going.Run the code inside the loop.
Repeat until we increase
i
to 4 on the fourth loop.Because
i
is no longer less thanrows
(4), stop repeating...