C# Tutorials - Herong's Tutorial Examples - v3.32, by Herong Yang
"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:
The following program uses a "for" loop to calculate the sum of integers from 1 to 10:
// ForLoops.cs
// Copyright (c) 2006 HerongYang.com. All Rights Reserved.
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
Table of Contents
Logical Expressions and Conditional Statements
Creating and Using Arrays - Example
Visual C# 2010 Express Edition
C# Compiler and Intermediate Language
Compiling C# Source Code Files
MSBuild - Microsoft Build Engine
System.Diagnostics.FileVersionInfo Class
WPF - Windows Presentation Foundation