for loops
for loops are special loops. The syntax might be a little bit confusing at first, but you will find yourself using them soon, because they are very useful.
Here is what the syntax looks like:
for (initialize variable; condition; statement) {
// code to be executed
}
Between the parentheses following the for
statement, there are three parts, separated by semi-colons. The first one initializes the variables that can be used in the for
loop. The second one is a condition: as long as this condition is true, the loop will keep on iterating. This condition gets checked after initializing the variables before the first iteration (this will only take place when the condition evaluates to true). The last one is a statement. This statement gets executed after every iteration. Here is the flow of a for
loop:
- Initialize the variables.
- Check the condition.
- If the condition is true, execute the code block. If the condition is false, the loop will...