Loops in C#

Another compulsory technique is looping while writing software - the ability to replicate a block of the X bar. In C #, they come in 4 different forms, and we will see each one of them.

while loop

The while loop is probably the easiest, so we will start with it. Unless the loop does not execute the block of code, then this condition is giving you which is true. A small example, and then a few more clarifications:


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 0;

            while(number < 5)
            {
                Console.WriteLine(number);
                number = number + 1;
            }

            Console.ReadLine();
        }
    }
}

Try running code You will find a good list of numbers from 0 to 4. The number is defined as 0 for the first time, and every time the code is executed in the loop, it grows by one, but why it gets only for 4 when code 5 is called? For the condition to return the condition, the number should be less than 5, which in this case means that the number which outputs in number is not accessible until the number is equal to 5. The reason for this is that when loop is evaluated before entering the status code block

do loop

On the contrary, it is suitable for do loop, which acts as a loop during other aspects. The loop evaluates the position after the loop is executed, which ensures that the code block is always executed at least once.


do
{
    Console.WriteLine(number);
    number = number + 1;
} while(number < 5);

Output is same - when the number is greater than 5, the loop gets out.

for loop

The for loop is slightly different when you know how many iterations you want, either because you know the correct amount of iterations, or because you have that amount in which the amount is the example of the loop here.


using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 5;

            for(int i = 0; i < number; i++)
                Console.WriteLine(i);

            Console.ReadLine();
        }
    }

It produces the exact same output, but as you can see, the loop is slightly more compact, it has 3 parts - we start to count a variable, set it to determine a conditional statement, And increase the counter (++ is the same "variable = variable + 1").

The first part, where we define the i variable and set it to 0, is executed only once, before the loop starts. The last two parts are executed for each frequency of the loop. Every time, I compare our numbers - if I am smaller than the number, then the loop runs once. After this, I have increased one

Try to run the program, and after that, try changing the number variable to be 5 to 5 or smaller. You will see the loop for the change.

foreach loop

The last loop, which we will see, is the foreach loop. It operates on the collection of objects, for example arrays or other underlying list types. In our example, we will use one of the simplest lists, which is called an Arrielist. It works a lot like an array, but do not worry, we will see it in the later section.


using System;
using System.Collections;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {            
            ArrayList list = new ArrayList();
            list.Add("John Doe");
            list.Add("Jane Doe");
            list.Add("Someone Else");
            
            foreach(string name in list)
                Console.WriteLine(name);

            Console.ReadLine();
        }
    }
}

Ok, so we make an example of an ArrayList, and after that we add some string items to it. We use foreground loop to run through each item, we set the name variable, which we have reached all the time. In this way, we have the specified variable for output. As you can see, we declare the string type variable name - you always need to tell the foreach loop, which you are hoping to remove from the data collection. If you have different types of lists, you can use the object class instead of a specific class to extract each object as an object.

While working with the archive, you have the possibility to use foreach loop in most of the time, mainly because it is easier for these tasks than any other loop.