C# For Beginner Tutorial – Loop Part

Welcome to third the tutorial At the end of this tutorial, we will learn about the while loop, for loop, for each loop, do-while loop, and break and continue statements.

Before starting the training, if you have not read the first 2 tutorials, you can move on to the first training from here and to the second training here.

List Of Topics To Learn

  • What is Loops?
  • While Loop
  • For Loop
  • For Each Loop
  • Do While Loop
  • Break and Continue Statements

What is Loops?

Loops are a structure used for repetitive operations. By creating a single loop instead of writing code over and over again, you avoid repeating a job.

For example, we increase the value of a variable we have and we want to print an even number on the screen whenever an even number value is assigned to the variable.

We can write Console.WriteLine one by one for this, but this is both laborious and increases the size of the program, so we use loops.

While Loop In C#

The while loop runs with a condition, it can run indefinitely if the condition you give is true, but if the result is false, it will stop or not work.

The syntax is similar to conditional statements, as long as the condition is true, the code in it will run continuously and will not pass to the code below.

using System;

class Program
{
   static void Main(string[] args)
   {
      int i = 0;
      while(i < 5)
      {
         Console.WriteLine(i);
      }
   }
}

Since the condition in the loop here will never be false, it creates an infinite loop. If i is increased every time the loop runs, the condition of the loop will break after a while.

using System;

class Program
{
   static void Main(string[] args)
   {
      int i = 0;
      while(i < 5)
      {
         i++;
         Console.WriteLine(i);
      }
   }
}
Output:
1
2
3
4
5

Since the loop runs as long as it is True, an endless loop can be created by typing true in the condition section.

For Loop In C#

The for loop loop is used more in programs, the difference from the while loop is not used to create an infinite loop (mostly) for loop is used more for finite loops.

A variable is created in the for loop, then this variable is added to a condition and it is determined whether to increase or decrease.

using System;

class Program
{
   static void Main(string[] args)
   {
      for(var i = 0; i < 5; i++)
      {
         Console.WriteLine(i);
      }
   }
}
0
1
2
3
4

The reason 5 is not written is that the value of i is increased by 1 before the loop starts. After the value of i increases, the loop is returned, then this process over and over again.

For Each Loop In C#

The For each loop is also one of the most used loops, working according to the relationship between two variables.

As an example, we have an array and we want to write the values in it one by one, in this case, for each can be used.

However, since we have not learned the concept of array yet, we will do this with the string variable, and we will solve examples with the data types we will learn in the next lesson.

using System;

class Program
{ 
    static void Main(string[] args)
    {
        string a = "Hello";
        foreach(var i in a)
        {
            Console.WriteLine(i);
        }
    }
}
H
e
l
l
o

If the values in two variables are equal, the value of in returns true, if there is no value in it, it returns false.

this loop works with indexable values like string, array, list, arraylist, but the important thing right now is that the variable i has written each letter of the string one by one and it ends the loop.

We’ll rework the For Each loop in the data types section of the other lesson. Now let’s move on to the other loop.

Do While Loop In C#

To tell you the truth, this cycle is a type of cycle that is not used at least, but it is useful to learn.

The code written in the do section is executed if the condition in the while is true, the loop continues to run until the while condition is false. It is very similar to a while loop.

using System;

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

        do
        {
            a--;
            Console.WriteLine(a);
        }while(a > 0);
    }
}
4
3
2
1
0

Break and Continue Statements

Sometimes we need to break loops or skip a certain section. We use a break and continue statements for these things.

using System;

class Program
{ 
    static void Main(string[] args)
    {
        int a = 0;
        while(a < 10)
        {
            a++;
            Console.WriteLine(a);
            if(a == 5)
            {
                break;
            }
        }
    }
}
1
2
3
4
5

Instead of continuing until 10, we ended the loop when the value of a is 5, and you can use these two statements in all loops.

using System;

class Program
{ 
    static void Main(string[] args)
    {
        for(var i = 10; i > 5; i--)
        {
            if (i == 7)
            {
                Console.WriteLine("7");
                continue;
            }

            Console.WriteLine("X");
        }
    }
}
X
X
X
7
X

As you can see, the following code did not work when the continue was run, and the loop was restarted. This because continues finishes this phase of the loop and runs the other part of the loop.

Leave a Reply

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