"for" Loop Statements

This section provides a tutorial example on how to use a 'for' loop statement to calculate the sum of integers between 1 and 10.

A "for" loop is a "for" statement, by which a group of embedded statements will repeatedly executed like a loop until the controlling condition reaches false. Here is the syntax of a "for" statement:

```for statement:
for (initiation_statement; logical_expression; iteration_statement) {
embedded_statements
)
```

When a "for" statement is encountered in an execution flow, it will follow the algorithm bellow:

• Step 1: Executed the initiation statement.
• Step 2: Evaluate the logical expression.
• Step 3: If the resulting value of the logical expression is true, run all the embedded statements; otherwise, end this "for" statement.
• Step 4: After executing the embedded statements, if they are executed as part of Step 3, executed the iteration statement, and loop back to Step 2.

The following program uses a "for" loop to calculate the sum of integers from 1 to 10:

```// ForLoops.cs

class ForLoops {
static void Main() {
long total;
total = 0;
int i;
for (i=1; i<=10; i=i+1) {
total = total + i;
}
System.Console.WriteLine("Sum of 1, 2, ..., 10: {0}", total);
}
}
```

Output:

```Sum of 1, 2, ..., 10: 55
```

Last update: 2015.