Loops are used to execute a block of code repeatedly until a specific condition is met. They are an essential part of any programming language.

C# provides 4 types of loops: the for loop, while loop, do-while loop, and foreach loop. Each loop has its own specific purpose and syntax.

For Loop

The for loop is used when you know the number of times you want to execute a block of code. It consists of three parts: initialization, condition, and increment/decrement.

for (initialization; condition; increment/decrement)
{
    // code to be executed
}

The initialization part is where you initialize a variable that controls the loop.

The condition part is a boolean expression that determines whether the loop should continue or not.

The increment/decrement part is used to modify the loop control variable after each iteration.

Here’s an example that prints the numbers from 1 to 5:

for (int i = 1; i <= 5; i++)
{
    Console.WriteLine(i);
}

Output:

1
2
3
4
5

While Loop

The while loop is used when you want to repeat a block of code as long as a certain condition is true. It checks the condition before executing the code block.

while (condition)
{
    // code to be executed
}

The condition is checked before every iteration, and if it evaluates to true, the code block is executed. If the condition is false initially, the code block is never executed.

Here’s an example of using a while loop in C#:

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

In this example, we initialize the variable i to 1. The while loop will continue executing the code block as long as the condition i <= 5 is true. Inside the loop, we print the value of i using Console.WriteLine(), and then increment i by 1 using the i++ statement.

When you run this code, it will print the numbers from 1 to 5 to the console.

Output:

1
2
3
4
5

Do-While Loop

The do-while loop is similar to the while loop, but it checks the condition after executing the code block. This guarantees that the code block is executed at least once.

do
{
    // code to be executed
} while (condition);

The code block is executed first, and then the condition is checked. If the condition evaluates to true, the loop continues, otherwise, it exits.

Here’s an example that prints the numbers from 1 to 5 using a do-while loop:

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

Output:

1
2
3
4
5

Foreach Loop

The foreach loop is used to iterate over elements of an array or a collection. It simplifies the process of accessing each element without the need for an index variable.

foreach (var item in collection)
{
    // code to be executed
}

The loop iterates over each item in the collection and executes the code block for each item.

Loop Control Statements

C# also provides loop control statements that allow you to control the flow of loops. These statements include break, continue, and goto.

The break statement

The break statement is used to exit out of a loop prematurely. It is commonly used to terminate a loop when a specific condition is met.

for (int i = 1; i <= 10; i++)
{
    if (i == 5)
    {
        break; // Exit the loop when i equals 5
    }
    Console.WriteLine(i);
}

In this example, the loop will print the numbers from 1 to 4 and then exit when the value of i becomes 5.

The continue statement

The continue statement is used to skip the remaining iterations of a loop and move on to the next iteration. It is commonly used to skip certain iterations based on a specific condition.

for (int i = 1; i <= 10; i++)
{
    if (i % 2 == 0)
    {
        continue; // Skip the current iteration if i is even
    }
    Console.WriteLine(i);
}

In this example, the loop will print only the odd numbers from 1 to 10. When i is even, the continue statement is executed, and the loop moves on to the next iteration without executing the remaining code within the loop body.

The goto statement

The goto statement is used to transfer control to a labeled statement within the same method. It is generally considered a less preferred way of controlling program flow and is often discouraged due to its potential to make code harder to understand and maintain. However, in certain situations, it can be useful.

int i = 1;
start: //<--- the "start" label
if (i <= 10)
{
    Console.WriteLine(i);
    i++;
    goto start; // Transfer control to the "start" label
}

In this example, the loop will print the numbers from 1 to 10 by using the goto statement. The program jumps back to the start: label after each iteration until i becomes greater than 10.

It’s worth noting that the goto statement should be used judiciously to avoid creating spaghetti code and to ensure the code remains readable.