The Wayback Machine - https://web.archive.org/web/20261029214300/https://www.geeksforgeeks.org/c-sharp/loops-in-csharp/

Loops in C#

Last Updated : 6 Oct, 2026

Loops in programming are used to execute a block of code repeatedly until a specified condition is met. They help reduce code duplication and make programs more efficient and readable. In C#, loops are essential for handling repetitive tasks.

  • Allow execution of code multiple times based on a condition.
  • Improve efficiency by avoiding repetitive code.
C#
using System;

class GFG
{
    static void Main()
    {
        for (int i = 2; i <= 10; i += 2)
        {
            Console.WriteLine(i);
        }
    }
}

Output

2
4
6
8
10

Types of Loops in C#

C# provides different types of loops that allow a block of code to be executed repeatedly based on a specified condition.

Loops
Types of loops

1. while Loop

The while loop executes a block of code repeatedly as long as the specified condition remains true. The condition is checked before the loop body executes.

Example: Program demonstrates a while loop that prints numbers from 0 to 10.

C#
using System;

class Geeks
{
    static void Main()
    {
        int i = 0;

        while (i <= 10)
        {
            Console.Write(i + " ");
            i++;
        }
    }
}

Output
0 1 2 3 4 5 6 7 8 9 10 

Syntax:

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

Flowchart:

While-loop
while-loop

Execution Flow

  • The while loop starts by checking the Boolean condition.
  • If the condition is true, the loop body executes.
  • The loop variable is normally updated inside the loop.
  • When the condition becomes false, the loop terminates.
  • Because the condition is checked before execution, it is an entry-controlled loop.

2. for Loop

The for loop is used when we know the number of iterations, meaning we know how many times we want to repeat a task. The for statement contains initialization, condition, and increment/decrement in one line.

Example: Program to demonstrates a for loop that prints numbers from 0 to 10.

C#
using System;

class Geeks{
    static void Main()
    {
        for (int i = 0; i <= 10; i++)
        {
            Console.Write(i + " ");
        }
    }
}

Output
0 1 2 3 4 5 6 7 8 9 10 

Syntax:

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

Flowchart:

Forloop
For loop

Executio flow:

  • Initialization: Initializes the loop variable and marks the beginning of the loop.
  • Testing Condition: Checks whether the loop should continue. The condition must return a Boolean value.
  • Statement Execution: If the condition is true, the statements inside the loop execute.
  • Increment/Decrement: Updates the loop variable for the next iteration.
  • Loop Termination: When the condition becomes false, the loop terminates.

Note: C# also provides the foreach loop, which is used to iterate through arrays and collections.

3. do-while loop 

The do-while loop executes its body first and checks the condition afterward. Therefore, the loop body is guaranteed to execute at least once.

C#
using System;

class Geeks{
    static void Main(){
        int i = 0;

        do
        {
            Console.Write(i + " ");
            i++;
        }
        while (i <= 10);
    }
}

Output
0 1 2 3 4 5 6 7 8 9 10 

Syntax:

do{
statements..
} while (condition);

Flowchart:

Do-while-loop
do-while loop

Execution Flow

  • The statements inside the do block execute first.
  • The condition is checked after the loop body executes.
  • If the condition is true, the next iteration begins.
  • When the condition becomes false, the loop terminates.
  • Therefore, a do-while loop executes its statements at least once and is an exit-controlled loop.

4. foreach Loop

The foreach loop is used to iterate through each element of an array or collection. It is useful when we want to access all elements without manually managing an index.

C#
using System;

class Geeks{
    static void Main(){

        string[] names = { "Sweta", "Gudly", "Amiya" };

        foreach (string name in names)
        {
            Console.WriteLine("Name: " + name);
        }
    }
}

Output
Name: Sweta
Name: Gudly
Name: Amiya

Syntax:

foreach (dataType variable in arrayOrCollection){
// code to be executed
}

Common Loop Mistakes and How to Avoid Them

If loops are not used correctly, they can introduce bugs that affect program performance, readability, and functionality. The following are common pitfalls.

1. Infinite Loops

An infinite loop is a loop that never terminates because its condition never becomes false.

Types of Infinite Loops

  • Infinite for loop
  • Infinite while loop
C#
using System;

class GFG
{
    static void Main()
    {
        for (int i = 0; i <= 5; i++)
        {
            Console.Write(i + " ");
        }
    }
}

Output

Output limit exceeded

2. Off-by-One Errors

An off-by-one error occurs when a loop executes one more or one fewer time than intended. This usually happens because the loop condition is not set correctly.

C#
using System;

class GFG
{
    static void Main()
    {
        for (int i = 0; i <= 5; i++)
        {
            Console.Write(i + " ");
        }
    }
}

Output

0 1 2 3 4 5 

Here, the loop runs 6 times, not 5 times.

3. Modifying Loop Variables Inside the Loop

Changing the loop variable inside the loop can cause certain iterations to be skipped or produce unexpected behavior.

C#
using System;

class GFG
{
    static void Main()
    {
        for (int i = 0; i < 5; i++)
        {
            if (i == 2)
            {
                // Modifies the loop variable
                // and skips the next iteration
                i++;
            }

            Console.WriteLine(i);
        }
    }
}

Output

0
1
3
4

4. Empty Loop Body

An empty loop body occurs when a loop iterates but does not perform any operation inside its body. This can make the code confusing and may indicate an unintended mistake.

C#
using System;

class Geeks
{
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            // Empty body - no operations
        }
    }
}

Output

No output

The program produces no output because the loop body is empty.

Comment

Explore