HASSANHABIB

§ 5.5 — C#: From Scratch

The Infinite ‘for-loop’

1 min read · chapter 7 of 12
Search the books

    Open the full search page →

    I didn’t want to put this in the ‘for-loop’ chapter because it would distract you from learning the basic ‘for-loop’.

    Let’s see what an infinite ‘for-loop’ looks like. Infinite ‘for-loops’ can be made with the annotation:

    for(; ; )

    It is an open-ended code style that runs your application forever.

    It would never reach your

    namespace Demo.App
    {
        class Program
        {
            static void Main(string[] args)
            {
                for(; ; )
                {
                    Console.WriteLine("Forever");
                }
    
                Console.ReadKey();
            }
        }
    }
    Console.ReadKey();

    Visual studio will even tell you that the point is unreachable code because this program will keep running forever.

            Console.ReadKey();
        }
    }

    If we run the program. It’s going to keep going until your computer runs out of memory.

    And that’s all there is to it!