2.13 Loops

Stasoz
5 min readApr 25, 2022

--

You can check this course on my website.

Loops allow you to execute a block of code repeatedly until a certain condition is met. C# has four different kinds of loops (for, while, do…while and foreach).

Loop For

Syntax:

initializer — is an expression that is calculated before the first execution of the loop body. Usually a local variable is declared here, which is a counter in the loop, but you can also declare other variables depending on the task.

condition — this is an expression that is checked before each new iteration of the loop, if it is true, the iteration starts running, if not then the loop stops running.

iterator — expression evaluated after each iteration. Iteration it is a one-time execution of a loop block. Usually, the value of the counter is incremented here.

Example:

Result:

Let’s see how the cycle works, step by step:

  1. Define variable int i = 1;
  2. Add a loop condition, i ≤ 3, since this is the first iteration and i = 1, then the condition 1 ≤ 3 is true and the first iteration of the loop is started — a construct Console.WriteLine(i) that outputs the value of i to the console.
  3. After executing the block of code, the first iteration ends and the third part of the loop is executed — i++. After that, the variable i will be equal to 2.
  4. The condition i ≤ 3 is checked again. It is true (because 2 is less than or equal to 3), so the loop block is executed again — Console.WriteLine(i).
  5. After executing the block of code, the second iteration ends and the third part of the loop is executed — i++. After that, the variable i will be equal to 3.
  6. The condition i ≤ 3 is checked again. It is true (because 3 is less than or equal to 3), so the loop block is executed again — Console.WriteLine(i).
  7. After executing the block of code, the third iteration ends and the third part of the loop is executed — i++. After that, the variable i will be equal to 4.
  8. The condition i ≤ 3 is checked again. It is false (because 4 is NOT less than or equal to 3), so the loop terminates execution.

You can also omit the first and third part of the loop, the main thing is not to forget to put a semicolon:

Result will be the same as in example above, because we have exactly the same loop exit condition and a counter variable i that is declared outside the loop which is incremented with each iteration.

And if we do not specify the condition for exiting the loop, then we get an infinite loop:

Finally, consider the example with several variables in the declaration of the loop:

First we define two variables i = 1 and j = 1 at the beginning of the loop, then add a loop exit condition i < 5 and specify that after each iteration, the value i and j incremented.

Inside the loop, we output the current iteration and the result of multiplying i by j.

Result:

Loop While

In a while loop, the condition is executed first, then the code execute if the condition is true. while is similar to for but includes only condition. Syntax:

Example:

Here we declare a variable i, which will be checked in the while loop condition and decrease with each iteration until the condition becomes false.

Result:

Loop do…while

The do…while loop in C# is the post-test version of while loop. This means that the loop condition is checked after the loop body is executed. Syntax:

The do…while loop should be used when you need to execute the code inside the loop at least once. Example:

Although we have a variable i less than 0, the loop will still be executed once.

Result:

Loop foreach

foreach loop is used to iterate over an array or collection of elements. Syntax:

Here, data_type variable means the type and name of the loop variable that receives element value of the collection at each iteration and collection represents the collection/array for iteration.

foreach works as follows — at the beginning of the loop, the first element of the collection is assigned to the loop variable, after each iteration, the following collection element is assigned to a loop variable. The loop runs until it goes through all the elements of the collection.

Example:

Here the collection is the string — “Hello”, because string is an array of characters. So we’re going to go through every string character. We also declared a variable item in the loop with a type char that must match to the type of collection elements.

Result:

We can also explicitly not specify the type of variable in the loop, but use the keyword var. In the case of using var, the compiler will determine the type itself. In practice, var is most often used than explicit type.

We have looked at how to iterate over a string in a foreach, but in the next sections, we’ll get to know arrays and collections and look at how to iterate over them.

Nested loops

Loops can be nested in others loops, I will show this with the example of a for loop:

Here the loop for (int i = 1; i <= 4; i++) has 4 iterations, but within each iteration, another loop for (int j = 1; j <= 5; j++) is executed with 5 iterations.

Result:

Contents

Previous article -> 2.12 Ternary operator

Join to our community in Telegram — https://t.me/itifico

If you want to support me, you can buy me a cup of coffee and I will drink it when I write the next article :)

--

--

Stasoz
Stasoz

Written by Stasoz

Full Stack Developer who is inspired by new technologies

No responses yet