"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) 2015, 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

Last update: 2015.

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

Arrays and Loop Statements

 Creating and Using Arrays

 Creating and Using Arrays - Example

"for" Loop Statements

 "while" Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

 WPF - Windows Presentation Foundation

 Partial Classes and Partial Methods

 Outdated Tutorials

 References

 PDF Printing Version