In this article, we are going to learn how to use loops in C#, what type of loops exist, and when to use them. We will use a couple examples to closely explain the usage of each loop in C#.

For the complete navigation of this series check out: C# Back to Basics.

If you want to download the source code for our examples, you can do that from here Loops in C# Source Code.

Let’s dig in.

In this article, we are going to talk about:

While Loop

While loop is a loop with a precondition. This means that we are checking a condition first and then if a condition returns true, we execute our expression:

Example 1: Create an application which calculates the sum of all the numbers from n to m (inputs from a user):

While example Loops in C#

So let’s explain the code above.

Because we calculate the sum of all numbers from “n” to “m”, we need to have a variable to store that value. It needs to be initialized with a zero at a beginning. Without that, our app will fail to build due to the sum variable being unassigned.

In a while loop, we are going through all the numbers from n to m and adding every number to the sum variable. We are using this expression: sum += n; which is a shorter for sum = sum + n;

Finally, we need to increment the n variable by 1. Without that, we would have an infinite loop because the value of the n variable would always be lesser than the value of the „m“ variable.

We should use while loops when the number of iterations is uncertain. This means that we could repeat iteration until some condition is fulfilled, but we are not sure how many iterations we would need to reach the condition fulfillment.

For Loop

For loop is another loop with a precondition. We use the following syntax to write it in C#:

We use initialization at the beginning of the loop and it serves the purpose of initializing the variable with a value. The condition is used to determine when the loop is completed. Progression is a part in which we increment or decrement our variable initialized in the initialization part. The body consists of all the expressions we need to execute as long as the condition is true.

It is important to know that the order of execution is: Initialization, Condition, Loop Body, Progression.

Example 1: Create an application which calculates the sum of all the numbers from n to m (inputs from a user):

For example Loops in C#

Example 2:  Create an application that prints out all the integer numbers from n to 1:

For example2 Loops in C#

We should use for loops when we know how many iterations we are going to have. This means if we iterate through all the elements inside a collection or we have an ending point for iterations.

Do-While Loop

The do-while loop is a loop with postcondition. What this means is that the loop body is executed first and the condition is checked after. Thats totally opposite from the previous loop examples.

We can implement this loop in the following way:

Example 1: Create an application which calculates the sum of all the numbers from n to m (inputs from a user):

Do-while example 1 Loops in C#

Example 2: Create an application which prints out the sum of all the even numbers to n:

Do-while example 2

Conclusion

Good job.

Now we can implement iterations in a combination with all that we have learned from the previous articles, thus making our application more powerful.

In our next post, we are going to talk about how to handle exceptions in C#.

If you have enjoyed reading this article and if you would like to receive the notifications about the freshly published .NET Core content we encourage you to subscribe to our blog.