How to create an infinite loop in C#?


An infinite loop is a loop that never terminates and repeats indefinitely.

Let us see an example to create an infinite loop in C#.

Example

using System;
namespace Demo {
   class Program {
      static void Main(string[] args) {
         for (int a = 0; a < 50; a--) {
            Console.WriteLine("value : {0}", a);
         }
         Console.ReadLine();
      }
   }
}

Above, the loop executes until a < 50. The value of is set to 0 initially.

int a = 0;

The value of a decrements after each iteration since it is set to.

a--;

Therefore the value of a will never be above 50 and the condition a <50 will be true always. This will make the loop an infinite loop.

Updated on: 23-Jun-2020

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements