How to create an infinite loop in C#?

An infinite loop is a loop that never terminates and repeats indefinitely. While infinite loops are generally undesirable in most applications, they can be useful in specific scenarios like game loops, server applications, or continuous monitoring systems.

There are several ways to create infinite loops in C#, each with different syntax and use cases.

Using For Loop

The most common way to create an infinite loop is by manipulating the loop condition so it never becomes false −

using System;

class Program {
    static void Main(string[] args) {
        int count = 0;
        for (int a = 0; a < 50; a--) {
            Console.WriteLine("value : {0}", a);
            count++;
            if (count >= 5) break; // Breaking to prevent actual infinite execution
        }
        Console.WriteLine("Loop terminated after 5 iterations");
    }
}

The output of the above code is −

value : 0
value : -1
value : -2
value : -3
value : -4
Loop terminated after 5 iterations

In this example, the variable a starts at 0 and decrements with each iteration (a--). Since a will always be less than 50, the condition a remains true indefinitely.

Using While Loop

A while(true) loop is the most straightforward way to create an infinite loop −

using System;

class Program {
    static void Main(string[] args) {
        int counter = 0;
        while (true) {
            Console.WriteLine("Iteration: " + counter);
            counter++;
            if (counter >= 3) break; // Breaking to prevent actual infinite execution
        }
        Console.WriteLine("While loop terminated");
    }
}

The output of the above code is −

Iteration: 0
Iteration: 1
Iteration: 2
While loop terminated

Using Do-While Loop

Similar to the while loop, you can create an infinite loop using do-while

using System;

class Program {
    static void Main(string[] args) {
        int i = 0;
        do {
            Console.WriteLine("Do-while iteration: " + i);
            i++;
            if (i >= 3) break; // Breaking to prevent actual infinite execution
        } while (true);
        Console.WriteLine("Do-while loop terminated");
    }
}

The output of the above code is −

Do-while iteration: 0
Do-while iteration: 1
Do-while iteration: 2
Do-while loop terminated

Common Infinite Loop Patterns

Here are the most common syntax patterns for creating infinite loops −

// Using for loop
for (;;) {
    // infinite loop
}

// Using while loop
while (true) {
    // infinite loop
}

// Using do-while loop
do {
    // infinite loop
} while (true);

Breaking Out of Infinite Loops

To exit an infinite loop, you typically use break statement with a condition −

using System;

class Program {
    static void Main(string[] args) {
        int userChoice = 0;
        while (true) {
            Console.WriteLine("Menu: 1-Continue, 2-Exit");
            userChoice++; // Simulating user input
            
            if (userChoice == 1) {
                Console.WriteLine("Continuing...");
            } else if (userChoice >= 2) {
                Console.WriteLine("Exiting loop");
                break; // Exit the infinite loop
            }
        }
        Console.WriteLine("Program ended");
    }
}

The output of the above code is −

Menu: 1-Continue, 2-Exit
Continuing...
Menu: 1-Continue, 2-Exit
Exiting loop
Program ended

Conclusion

Infinite loops in C# can be created using for, while, or do-while loops with conditions that never become false. While useful for specific scenarios like continuous monitoring or game loops, always ensure you have a proper exit condition using break statements to prevent unintended infinite execution.

Updated on: 2026-03-17T07:04:35+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements