By 22/07/2015 11 Comments

C# For Loop

  • It is called a basic loop.
  • It is a flexible and powerful loop in C#.
  • It iterates or repeats a statement for given number of time or known time of repetition.

Syntax:

for (declaration / initialization ; condition ; iteration / updation)
{
statement (s)…
}

STATEMENT DESCRIPTION
for (declaration / initialization ; condition ; iteration / update) Declare for loop using for keyword. NOTICE, for loop has three parts, first one is declaration / initialization which defines a local variable with value or without a value which can be any integral data type such as int count = 1 this part also called starting point of a for loop. The second part is a (condition) which defines any bool condition true or false such as (count<=10), as long as this condition becomes true each time, than for loop will repeat the execution of given code or statement inside its body until that becomes false. The third (last one) part is an iteration or update which defines the iteration or modification of any integral data type’s value by using binary operators such as +=, ++, –, +, / etc.
{ This parenthesis indicates the starting scope or body of for loop, inside this scope you can specify any multiple statements, expression or anything which do you want to be repeated by for loop. NOTE: This parenthesis is an optional, if you do not specify it than only single statement or expression will be repeated by for loop, but if you want to repeat more than one statements or expressions than you must specify these parenthesis.
statement (s)… Anything or any statement to be repeated by for loop.
} This parenthesis indicates the closing scope or block of for loop.

Example-1:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            for (int count = 1; count <= 10; count++)
            { // Start for loop scope.

                // Print this line 10 times, because above loops executes 10 times.
                Console.WriteLine("Line: {0}", count);

            } // End for loop scope.

            Console.WriteLine("for loop has been terminated.");
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   Line: 1
   Line: 2
   Line: 3
   Line: 4
   Line: 5
   Line: 6
   Line: 7
   Line: 8
   Line: 9
   Line: 10
  */
}

Example – 2:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // A local variable is initialized outside from loop's body.
            int count = 1;

            for (; count <= 10;)
            { // Start for loop scope.

                // Print this line 10 times, because above loops executes 10 times.
                Console.WriteLine("Line: {0}", count);

                // loop's updation part.
                count++;

            } // End for loop scope.

            Console.WriteLine("for loop has been terminated.");
            Console.ReadKey();

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   Line: 1
   Line: 2
   Line: 3
   Line: 4
   Line: 5
   Line: 6
   Line: 7
   Line: 8
   Line: 9
   Line: 10
  */
}

Example – 3:

using System;
namespace csharpBasic
{
    // Start class definition / declaration.
    class Program
    {
        // Static main method void type declaration.
        static void Main(string[] args)
        {
            // An infinite for loop, this loop will never be stoped/terminated. 
            for (; ; )
            { // Start for loop scope.

                // Print following lines infinite times.
                Console.WriteLine("I am an infinite for loop....");

            } // End for loop scope.

        } // End of main method definition. 
    } // End of class. 

    /*
   The Output will be:
   I am an infinite for loop....
   I am an infinite for loop....
   I am an infinite for loop....
   ...............................
  */
}





Next Tutorial →


← Previous Tutorial


Posted in: C# Basics, C#.NET

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .