Foreach is used to iterate over a collection or an array of objects or values; it can be anything from a simple list of values to a rows in a dataset table.
When using a foreach loop, you don’t have to know the number of elements that the loop will iterate over because it will iterate over all elements in an array or a collection, unless you explicitly end the loop prematurely.
The following code will loop over the animals in the array.
string[] animals = { "cat", "dog", "bird" };
foreach (string animal in animals)
{
// Code to execute
}
A for loop is a way to iterate over a set of values until the given expression evaluates to false. A for loop has three parts: a start value for the loop, an expression telling the loop when to stop, and a counter.
[int i = 0] is the start value, [i < 100] is the condition that makes the loop iterate 100 times, [i++] is the iterator.
for (int i = 0; i < 100; i++)
{
// Code to execute a...
50% Complete
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.