|
A for loop (like in other languages) executes its block until its condition is met. Unlike other structures, though, it has a built-in way to initialize variables to a certain value, and a bit of code to run each time before it repeats. A typical for loop looks like this:
for (initialization ; condition ; incrementation) { some code }
The initialization section is normally used to set a variable to some known value (like zero) and is run as soon as the interpreter reaches it, before anything else. It is run only once.
Next, the interpreter checks the condition which works just like an if statement. The for loop will execute its code block until so long as this statement remains true.
Each time the interpreter reaches the end of the code block, it executes the incrementation section, which is normally used to add something to (increment) a variable so that at some point the condition forces the loop to end.
An in-depth (including assembly) explanation of exactly how this works is available here.
|