Code

foreach loop

The foreach loop provides simple syntax to cycle through all the elements in an array or collection, unless you explicitly end the loop with the break command.

foreach loop structure foreach loop execution order
foreach loop foreach loop execution order

Syntax

The syntax of the foreach loop is as follows:

foreach (type item in collection)
{
        Statements;
}

With a single statement

Curly brackets { } for the loop are optional if there is only one statement in the code block.

int[] integerValues = { 1, 2, 3, 4 };
foreach (int value in integerValues)
    System.Console.WriteLine(value); // 1234

Run Demo

OUTPUT

1
2
3
4

With a block statements

If you have more than one statement within the loop, you must enclose the statements in opening and closing braces { }. You can use braces with the single statement as well.

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
    System.Console.WriteLine(value); // 1234
}

Run Demo

OUTPUT

1
2
3
4

In the code above, you have an int variable value that is used for looping. Each time the loop runs, an element in the integerValues array is assigned to the variable value. For example, the first time the loop runs, the number 1 is assigned to value. It then executes the code within the block that makes up the foreach loop body. The line System.Console.WriteLine(value); simply prints out the number 1 on the Console. In the second iteration, the number 2 is assigned and printed out. This continues until all the elements in the array have been printed. This means that the code inside foreach loop is called as many times as there are elements in integerValues array. So, if integerValues contains 4 elements, the code inside the loop block will be executed 4 times.

foreach (int value in integerValues)
{
    System.Console.WriteLine($"First Statement : {value}\n");
    System.Console.WriteLine($"Second Statement : {value}\n");
}

Run Demo

OUTPUT

First Statement : 1
Second Statement : 1
First Statement : 2
Second Statement : 2
First Statement : 3
Second Statement : 3
First Statement : 4
Second Statement : 4

More Examples

List<string> Vegetables = new List<string> { "Carrot", "Cucumber", "Tomato", "Onion", "Green Pepper" };

foreach (string veg in Vegetables)
    System.Console.WriteLine(veg + "\n");

Run Demo

OUTPUT

Carrot
Cucumber
Tomato
Onion
Green Pepper

public class Planet
{
	public int Id { get; set; }
	public string Name { get; set; }
}
var planets = new List<Planet>
{
    new Planet() {Id = 1, Name ="Mercury"},
    new Planet() {Id = 2, Name = "Venus"},
    new Planet() {Id = 3, Name = "Earth"}
};

foreach (var planet in planets)
{
    Console.WriteLine($"{planet.Id}, {planet.Name}");
}

Run Demo

OUTPUT

1, Mercury
2, Venus
3, Earth

Note that the iterator variable is read-only and can’t be used to change elements in the array.

break Statement

foreach loop break statement

This code writes out the numbers 1 and 2 because the break command causes the loop to exit when value reaches 3.

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
    if (value == 3) // <- A
        break;

    Console.WriteLine(value);
}

Run Demo

OUTPUT

1
2

continue Statement

foreach loop continue statement

First we initialized an array and assigned value to it. When value = 3, the condition marked A evaluates to true. This causes the foreach loop to skip rest of the current iteration and initiates the execution of the next iteration.

int[] integerValues = { 1, 2, 3, 4 };

foreach (int value in integerValues)
{
    if (value == 3) // <- A
        continue;

    System.Console.Write(value);
}

Run Demo

OUTPUT: 3 skipped

1
2
4

Add Comment

Click here to post a comment

Subscribe

Most Popular

Popular Video