See, the syntax of the for loop is so:
1 2 3 4
|
for (initialization; condition; increment/decrement/changes in some variable)
{
//Your code
}
|
If the condition is true, it executes the code (iteration), and then makes some changes in the variable. If your condition is still true after changing the variable, it executes the code again (re-iteration). The moment the condition becomes untrue, it will not re-iterate and exit the for loop.
So n won't ever reach 5, in the first call of your function where length=3. It'll take value 2 (after 0 and 1), 2<3 is still
true, so it will execute, increment n, n will take value 3, 3<3 is
False, and hence it will exit.
For length=5, it will take value 0, 1, 2, 3, 4 and execute the code for each. At n=5, it will exit since 5<5 is false...