How To Use For Loop In C#
In this article, I will explain how the for Loop can be used in C#.
Introduction
For loop is used to run a statement or a block of statements repeatedly until a specified condition has been reached. For loop is useful where you know in advance how many times you want the loop to iterate. The syntax for the For loop is as follows.
Syntax
for (initializer; condition; iterator)
{
//body
}
Initializer is the initialize clause in which the loop iterators are declared.
Condition contains a boolean expression that's evaluated to determine whether the loop should exit or should run again.
Iterator is to increment/decrement the value that is executed after each iteration.
Example
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
class WhileTest
{
static void Main()
{
int r = 1;
int s = 10;
int sum = 0;
for (r = 1; r < 10; r++ )
{
sum = r + s;
Console.WriteLine(" value of sum is {0}", sum);
}
Console.ReadLine();
}
}
The output of following program
