ASP.NET Basics (Part 5): Cooking Up a Storm - Getting Lucky
(Page 7 of 8 )
So far, I have used the "for" loop to navigate through the contents of an array. But there's more than one way to skin a cat...and so, C# also lets you iterate over an array with a new loop construct, one I conveniently avoided explaining last time. It's called a "foreach" loop, and it looks like this:
foreach (element in array) { do this! }
Here is a simple example for the "foreach" loop.
<SCRIPT language=c# runat="server">
void Page_Load()
{
// define the type of array
int [] lucky_numbers;
// initialize array
lucky_numbers = new int[5];
// assign values to each element
lucky_numbers[0] = 3;
lucky_numbers[1] = 6;
lucky_numbers[2] = 9;
lucky_numbers[3] = 21;
lucky_numbers[4] = 27;
// use the foreach loop
// to iterate over each element
foreach(int lucky_number in lucky_numbers)
{
output.Text = output.Text + " <b>" + lucky_number + "</b>";
}
}
</SCRIPT>
Here are my lucky numbers: <asp:label id=output runat="server"></asp:label>. What are yours?
This is what you should see in our browser.
Here, I've first defined an array of integers called "lucky_numbers", to store a set of numbers. Next, I've used the "foreach" loop to iterate over the loop and print each element of the array.
<%
// snip
// use the foreach loop
// to iterate over each element
foreach(int lucky_number in lucky_numbers)
{
output.Text = output.Text + " <b>" + lucky_number + "</b>"; }
// snip
%>
Each time the loop runs, it stores the current element of the array in the "lucky_number" instance variable. Once the loop runs out of its elements to process, it automatically exits and control passes to the line following it.
How about iterating over a two-dimensional array? Take a look:
<SCRIPT language=c# runat="server">
void Page_Load()
{
// define an array
string [,] meals;
// initialize array with two dimensions
meals = new string[3,3];
// assign values to each element
meals[0,0] = "vegetable soup";
meals[0,1] = "cream of mushroom soup";
meals[0,2] = "chicken soup";
meals[1,0] = "lasagna";
meals[1,1] = "pasta";
meals[1,2] = "tandoori chicken";
meals[2,0] = "chocolate mousse";
meals[2,1] = "tiramisu";
meals[2,2] = "apple pie";
foreach(string dish in meals)
{
output.Text = output.Text + " " + dish;
}
}
</SCRIPT>
<asp:label id=output runat="server"></asp:label>.
This is the output.
As you can see, the "foreach" loop does not give me as much control over the array, as compared to the two "for" loops demonstrated in the earlier example.
Thus far, the number of columns in the array was equal to the number of rows. However, this is not a hard and fast rule - it is possible to define a multi-dimensional array which is not so symmetrical in its dimensions, such as
books
[10,4];
or
authors
[30,5]
However, using an array with three or more dimensions leads to complex programming logic, and code that is difficult to write, read and debug; it should therefore be avoided as far as possible.
Next: Jagged Little Pill >>
More ASP.NET Articles
More By Harish Kamath (c) Melonfire