Topic : For Loop In C# Programming:



For Loop:

For Loop is a repetition control structure that allows us to efficiently write a loop that needs to execute a specific number of times. The for loop or statement encloses one or more C# statements that form the body of the loop. These statements in the loop continuously repeat for a specified number of times.



for Loop

Syntax:

The Syntax of the for loop is:



 for(Initialization; condition; Process)
    {
      /*  Block of one
         Or more C#
         Statements: */
    }



Inside the Parentheses () of for statement, semicolons (;) separate three expressions. Initialize, condition, and process:


  • » Initialize: This step is executed first, and only once. this step allows us to declare and initialize any loop control variables. we are not required to put a statement here, as long as semicolon appears.
    » Condition: Next, the condition is evaluated. if it is true, the body of the loop is executed. if it false, the body of the loop does not execute and flow of control jumps to the next statement just after the for-loop (if any).
    » Process: After the body of the for-loop executes, the flow of control jumps back up to the "Process". this statement allows us to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.


The condition is now evaluated again. if it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). after the condition becomes false, the for loop terminates.



Example:

Let's have an short and easy example to clear this concept. in this example we will print first 10 digits. (1-10).



/*Example: For Loop: InfoBrother: */

using System;

namespace forLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            int i;

            for(i=0; i<10; i++)  //print digits from 0 to 9:  
            {   //loop body begin.
                Console.WriteLine("The Value of i is: " + i);
            }   //loop body close.

            Console.ReadKey();
        }
    }
}


In for-loop all three statements must be seperated by semicolon (;)


We can initialize and declare a variable at a same time in for-loop Parentheses; as shown below: but note that there will be no more variable (i.e. "i") outside from the body of for-loop, as it declare only for "for-loop".



/*Example: For Loop: InfoBrother: */

using System;

namespace forLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            for(int i=0; i<10; i++)  //Declare and initialize i at the same time.  
            {   //loop body begin.
                Console.WriteLine("The Value of i is: " + i);
            }   //loop body close.

            Console.ReadKey();
        }
    }
}


Example:

Let's have another useful Example which will show some uses of for-loop: in this example we will create Math Multiplication Tables. This Program can create table of any number, and up to any number. this program will ask to user, "for what number you need table" you will enter any number for making table, and next it will ask you to enter the Maximum limit (limit to end table,). it will start from 1, and will end at MaxLimit.
Here we Need three variables having type int.


  • » Number: Select any number to Make table:
    » MaxLimit: select table limit: it will start from 1 and will end at MaxLimit that you selected.
    » Counter: it will used for loop-statement. to show the counter from 1 to Maxlimit.



/*Example: For Loop: InfoBrother: */
/* Table: Number X Counter = result: */

using System;

namespace forLoop
{
    class Program
    {
        static void Main(string[] args)
        {
            int number, counter, maxLimit;

            Console.WriteLine("For what Number you want to make table?");
            number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine("Enter Maximum Limit for Table:");
            maxLimit = Convert.ToInt32(Console.ReadLine());


            /*Start table from counter (i.e. 1), and continue it till maxLimit: */
            for(counter = 1; counter<= maxLimit; counter++)
            {
                Console.WriteLine("{0} X {1} = {2}", number, counter, number * counter);
            }



            Console.ReadKey();
        }
    }
}


The for-loop most frequently is used as a count-controlled loop, or one whose execution is controlled by counting the number of repetitions. Although the initialize portion of a for loop can contain multiple statements, some programmers believe the best style is to use only the loop control variable in the for statement and to insert other statements before the loop begins.

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement