For loop
The for statement creates a loop which repeatedly executes its contents until a condition is no longer true. It is functionally equivalent to a while loop, but its syntax allows for a more convenient way to define an iterator variable and an iterator increment expression.
For loops can be the target of break and continue statements.
Syntax
The for loop is made up of four distinct parts, three of which are inside of the round brackets. The components in the round brackets are separated by semicolons. Any of the components may be left out.
The first component can define an iterator variable which is only accessible inside the for loop body. The second component is executed before every iteration, and provides the condition which determines whether the for loop should continue executing. If the condition evaluates to false, the for loop exits. The third component is executed at the end of every iteration, and is usually used to increment the variable defined in the first component. Finally, the for loop body is located after the closed round bracket, and is executed on every iteration.
Example
This example calculates the sum of an array's contents, by iterating over its contents, reading each value in the array, and adding it to the Sum variable.