Text Size

FOR LOOPS IN C#

PDF 

Tutorials

(1 vote, average: 5.00 out of 5)

INTRODUCTION

C# provides several mechanisms for flow control in a program.  The loops in C# allow you to execute a block of code repeatedly, until a certain condition is met. This condition can be the number of repetition, a variable taking a specified value or something completely different. The for loop allows you to repeatedly execute a block of code for a specified number of times.

 

SYNTAX AND USE

The syntax of a for loop is the following one:

 

for (initializer; condition; iterator)

Statements;

 

Where

  • The initializer is the expression that is evaluated before the execution of the first loop. It is usually an initialization of a local counter variable that counts the number of executions/iterations.
  • The condition is checked before the start of another iteration. If this condition statement results to true, another iteration will take place. Otherwise the loop ends and the programs proceeds normally. This is usually an inequality check of the variable defined in the initialization part of the loop to a specific value or another variable.
  • The final statement, iterator is evaluated after the completion of an iteration. It usually alters the variable defined in the initialization part of the loop. The iterations end when the condition statement result to false.

 

The condition statement is checked for every iteration, including the first one. It is possible that the loop won’t execute at all, not even once. This is possible if, at the first evaluation (before the first iteration takes place) the condition statement evaluates to false. For this reason the for loop is called a pre-test loop. For loops can replace a repetitive block of code and thus make your code much more compact and efficient.  Also, new compilers optimize memory and data access very efficiently if you have for loops in your code. The following example counts and writes in the console the numbers from 0 to 99:

 

static void Main(string[] args)

        {

            for (int i = 0; i < 100; i++)

                Console.WriteLine(i.ToString());

 

        }

 

You do not have to use an end statement for the for loop.  Only the line after the declaration of the for loop is considered to be included in it. If you want to insert a block of code inside the loop you should add curly braces ({}):

 

        static void Main(string[] args)

        {

            for (int i = 0; i < 100; i++)

            {

                Console.WriteLine(i * 20);

                Console.WriteLine(i.ToString());

            }

            Console.ReadLine();

        }

 

The i ++ statement is identical to the statement  i = i + 1. The loop starts by initializing the integer type variable to zero. Since zero is less than one hundred, an iteration takes place and then the variable is increased by one. The condition is evaluated again and another iteration takes place. When the variable i is assigned the value 100 the condition returns false and the loop execution stops.

It is also possible to use nested loops, a for loop inside another for loop. This is particularly useful when iterating the elements of a two-dimensional table or array line by line or row by row. Finally, you could omit one or even all of the expressions in the for loop. However, in these situation you are better off by using a while loop.

Trackback(0)
Comments (0)add comment

Write comment

busy

Site Statistics

Stats
Total Members
: 542
Total Discussion
: 0
Total Albums
: 0
Total Photos
: 0
Total Bulletins
: 0
Total Activities
: 1
Total Wall Posts
: 1