for loop and foreach loop in C# with examples


For loop and foreach loop in C#: in this article, we will discuss very important looping statements which is for loop and foreach loop.

for loop in C#:

The for loop is another loop variant in which the three statements are used to process the loop variables (Initialization, condition and change) are pulled together in the loop head – what makes the for loop very clear compared to the while loop.

Syntax of for loop in C#:

The for loop in c# is mainly used when the number of loop iterations is already in place of the code is established. This was, for example, in our introductory while loop example for Calculating the powers of the case. The same code as a for loop would look like this:

At the beginning of the loop, the initialization statement from the loop head is executed. In the above Loop, this means that the loop variable n is defined and set to 0. Then the statements in the loop are executed repeatedly for as long as those in the loop head formulated condition – in the above loop n <= 10 – is fulfilled. After each loop pass, the third and last instruction from the loop head, the change instruction, executed, which increments the loop variable in the above loop (++ n).

For loop Empty Statement in C#:

If you initialize, check or change the loop variables elsewhere than in the If you want to make the head of the for loop, just add it in the appropriate place in the loop head an empty statement (;).

This loop only defines the condition in the loop head. The initialization of the loop variables happens further up in the application, the change within the loop block.

Declare loop variable inside loop head

You can also declare the loop variable directly in the initialization part. It is then only in the head and the Statement block of the loop is valid and is automatically resolved when the block is exited.

However, the variable defined in a loop must not have the same name as a local variable carry the surrounding block!

Note For C ++ and Java programmers: In general, in C#, local variables with the same name are in nested Blocks do not obscure each other, but lead to a compiler complaint. C# does not allow multiple loop variables in the for loop.

The foreach loop for arrays and collections

A common area of ‚Äč‚Äčapplication for loop in c# is iterating through all elements in an array or a list. C# provides a special variant of the for loop for this which is foreach loop.

foreach loop in C#:

foreach loop syntax in C#:

In the loop head, you define a variable of the type of elements in the list. You also give the listing itself. That’s it!

The loop then automatically loops through all of the items in the collection. At the beginning of each iteration, the next element is saved in the variable you have defined. Then the loop block executed.


The variable specified in the loop head is not a loop variable. It is not used for loop in c# control, but only presents the current item from the listing. You can also use the variable to view the current element only query, but not change! For situations where you need a loop variable or even write access to the elements, the foreach Loop therefore not suitable.

Iterate through elements of an array:

Iterate through elements of a collection:

Execution tips:

If the execution speed of an application is to be improved, the optimization of the loops always comes first. By iterating the instruction blocks of the loops several times even small tweaks in these instructions can lead to a significant improvement the run time.

Here is a small checklist:

  • Take any unnecessary statements out of the loop.
  • Check all statements in which no loop variable appears to see whether you can use these statements cannot be placed in front of the loop as initialization or behind the loop for post-processing.
  • As far as possible, avoid time-consuming operations (input and output in files, screen outputs, Object creation).
  • Avoid creating temporary objects in the loop (better: an object before the Create a loop and then use it in the loop).

For loop and foreach loop examples in C#:

Example1: how to use simple for loop in c#:

for loop in c#

Example2: how to use nested for loop in c#:

for loop in c#

Example3: how to use foreach loop in c#:

for loop in c#


Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button