C# for loop
C# loop used to execute same block of code again and again specified number of time.
C# for loop – In for loop execute block of code up to the condition is true.
In for loop condition is predefined.
for loop syntax in C#
for ( initialization; condition; increment/decrement)
{
// block of code to be executed
}
for (int i = 0; i < length; i++)
{
// block of code to be executed
}
There’s three parts of code in for loop bracket:
- int i=0 : The number you want to start.
- i<length : The condition define how many times for loop go round and round.
- i++ : increment or decrement in start counter value each time round in loop.
Adding for loop in C# .Net
Open visual studio and create new website with c#. Open a web form code part and right click anywhere between brackets of the page load portion. On the right click menu select Insert Snippet and click on it as shows in below figure.
After click on Insert Snippet option from menu, you will see a list of items, Now double click on Visual C#.
After double click on Visual C# option from items, you have list of snippets, Now double click on for.
After double click on for option from list of snippets, you will have code of for loop on your web page.
Initialization (int i=0) = used to set start counter.
Condition (i<length) = if condition is true for loop will continue, if condition is false the for loop terminated.
Increment/Decrements (i++) = used to increment or decrement start counter.
The initialization define the starting point of loop, In initialization portion we declare variable with value and then check the condition. If the condition is satisfied with the initial value then the block of code to be executed. After first execution of code the increment or decrements will be done as defined in increment/decrement portion of for loop.
After increment initial value check condition this loop execute again and again while the condition is true. When the condition is not satisfied means condition is not true the for loop terminated.
Example of for loop in C#
for (int i = 1; i <= 5; i++)
{
Response.Write(i);
}
The output is:
12345
In above for loop example we declare integer variable i with value 1, here the condition is i<=5, for i=1 the condition is true then execute code print i.
Then increment 1 to i,
now i=2 the condition is true,
i=3 condition is true,
i=4 condition is true.
i=5 condition is true.
When i=6 the condition i<=5 is not true, the for loop terminated this time.
so for loop execute code five time and out put is : 12345
for loop example in C#
for (int i = 5; i > 0; i--)
{
Response.Write(i);
}
The output is:
54321
Here, we start count with i=5, check condition i>0 after decrements 1 to start counter.
C# for loop Example
string name="Meera Academy";
for (int i = 1; i <= 5; i++)
{
Response.Write("Hello " + name + "<br />");
}
The output is:
Hello Meera Academy
Hello Meera Academy
Hello Meera Academy
Hello Meera Academy
Hello Meera Academy
C# for loop example
int sum = 0;
for (int i = 1; i <= 5; i++)
{
sum = sum + i;
}
Response.Write("The sum of 1 to 5 = " + sum);
The output is:
The sum of 1 to 5 = 15
C# for loop example
for (int i = 1; i <= 10; i = i + 2)
{
Response.Write(i);
}
The output is:
13579
Example to display odd and even numbers.
string even = "";
string odd = "";
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
even = even + " " + i;
}
else
{
odd = odd + " " + i;
}
}
Response.Write("The Even no = " + even + "<br/>");
Response.Write("The Odd no = " + odd);
The output is:
The Even no = 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
The Odd no = 1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49
C# Example of Nested for loop
C# supports Nested for loop – for loop inside another for loop.
for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
{
Response.Write(i);
}
Response.Write("<br/>");
}
The output is:
1
22
333
4444
55555
C# Example of Nested for loop
for (int i = 1; i < 6; i++)
{
for (int j = 1; j <= i; j++)
{
Response.Write(j);
}
Response.Write("<br/>");
}
The output is:
1
12
123
1234
12345
Exit the for Loop
The break keyword is used to stop and exit from a for loop.
for (int i = 1; i <= 10; i++)
{
if (i > 5)
{
break;
}
Response.Write(i);
}
The output is:
12345
Related ASP.Net Topics :
Do-While Loop in C#.Net
Nested If Statement in C#.Net
Subscribe us
If you liked this c#.net post, then please subscribe to our YouTube Channel for more c#.net video tutorials.
We hope that this asp.net c# tutorial helped you to understand about for loop.
Next asp.net tutorial we will learn about C# While Loop.