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.
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:
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();
}
}
}
For Loop
The Value of i is: 0
The Value of i is: 1
The Value of i is: 2
The Value of i is: 3
The Value of i is: 4
The Value of i is: 5
The Value of i is: 6
The Value of i is: 7
The Value of i is: 8
The Value of i is: 9
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.
/*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();
}
}
}
For Loop
For what Number you want to make table?
5
Enter Maximum Limit for Table:
10
5 X 1 = 5
5 X 2 = 10
5 X 3 = 15
5 X 4 = 20
5 X 5 = 25
5 X 6 = 30
5 X 7 = 35
5 X 8 = 40
5 X 9 = 45
5 X 10 = 50
“
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.