ASP.NET for loop, foreach loop and While loop


For Loop

If you need to run the same statements repeatedly, you can program a loop.

If you know how many times you want to loop, you can use a for loop. This kind of loop is especially useful for counting up or counting down:

Syntax of For loop

  
for(initialization; condition; step)
	statement
  • initialization : Initialize the value of variable.
  • condition: Evaluate the condition
  • step: Step taken for each execution of loop body

Example of for loop

C# code

  
using System;

public partial class _Default : System.Web.UI.Page 
{
	protected void Button1_Click(object sender, EventArgs e)
	{
		int starVal = 1;
		int maxVal = 10;

		for (int i = starVal; i <= maxVal; i++)
		{
			Label1.Text = i.ToString();
		}
	}
}

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>

Output

1 2 3 4 5 6 7 8 9 10

For Each Loop

The foreach loop executes a block of code on each element in an array or a collection of items. When executing foreach loop it traversing items in a collection or an array .

Syntax of foreach loop

  
foreach(Item in Group){
	// loopBody
  }
  • Item : The Item in the group
  • Group : The group containing items

Example of for loop

C# code

  
using System;

public partial class _Default : System.Web.UI.Page 
{
	protected void Button1_Click(object sender, EventArgs e)
	{
		string[] days = { "Sunday", "Monday", "TuesDay", "Wednesday", "Thursday", "Friday", "Saturday" };
		ListBox1.Items.Clear();
		foreach (string day in days)
		{
			ListBox1.Items.Add(day);
		}
	}
}
	

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
	</div>
	</form>
</body>
</html>

Output

Sunday Monday TuesDay Wednesday Thursday Friday Saturday

While Loop

The while loop is a general purpose loop.

A while loop begins with the while keyword, followed by parentheses, where you specify how long the loop continues, then a block to repeat.

While loops typically add to, or subtract from, a variable used for counting.

Syntax of while loop

  
 while (expression)
  {
	statement ;
  }

Example of while loop

C# code

  
using System;

public partial class _Default : System.Web.UI.Page 
{
	protected void Button1_Click(object sender, EventArgs e)
	{
		int count = 1;
		while (count <= 10)
		{
			Label1.Text = "The value of i is : " + count;
			count = count + 1;
		}
	}
}	

HTML Code

<html>
<body>
	<form id="form1" runat="server">
	<div>
		<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
		<br />
		<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
	</div>
	</form>
</body>
</html>

Output

The value of i is : 10

Share this article on