none
How do I create an infinite loop

    Question

  • Hello all,

    How do I create an infinite loop? I tried "for (int i = 1; i++)" with no luck. I would also like to know how to end the loop by hitting any key  I would assign.

    Regards,

    Ken


    ken3663
    Monday, January 24, 2011 1:21 AM

Answers

  • For the loop, use while instead of for:

    while (true)
    {
    }
    
    For checking for key presses, that depends on what your application. Is is a Console App (use Console.Read), is it a WinForm app (use an event handler for any of the key events: KeyDown, KeyPress, KeyUp), is it a WPF app (I don't do WPF, so I don't know what you'd use there).
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 1:40 AM
    Monday, January 24, 2011 1:30 AM
    Moderator
  • You don't need to add "true" in the for statement:

    for( ; ; )
    {
    	Console.WriteLine("looping...");
    
    	if (Console.KeyAvailable)
    	{
    		break;
    	}
    }

    If if remember well, there was a FOREVER macro defined just like that in the headers for my C compiler when I programmed on the Amiga.

    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:29 AM
    Monday, January 24, 2011 1:31 PM
  •  for (int i = 0; i-1 < i++; i++)
          {
            MessageBox.Show("oh dear");
          }
    
    That worked for me.
    • Marked as answer by ken3663 Monday, January 24, 2011 1:42 AM
    Monday, January 24, 2011 1:30 AM
  • Hello,

    you can also do that like this.

    for (; !Console.KeyAvailable; )
    			{
    				Console.WriteLine("looping...");
    			}
    
    for (; true; )
    			{
    				Console.WriteLine("looping...");
    
    				if (Console.KeyAvailable)
    				{
    					break;
    				}
    			}
    


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    • Marked as answer by ken3663 Monday, January 24, 2011 2:21 AM
    Monday, January 24, 2011 1:46 AM
    Moderator
  • While Merlin's loop will work, I wouldn't recommend doing it that way for two reasons:

    1) It's not obvious that you are actually doing an infinite loop (and that you actually did mean to do an infinite loop).

    2) It's inefficient to be constantly incrementing i

    The while(true) syntax makes it clear that you actually did mean to do an infinite loop and it is quite efficient.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 2:01 AM
    Monday, January 24, 2011 1:47 AM
    Moderator
  • Hello Bonnie ,

    I'm new to programming. while (true) worked, thank you.

    This is a Console App and I tried Console.Read but I couldn't figure it out. If you have any other advice I would much appreciate it.

    Regards,

    Ken


    ken3663
    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:27 AM
    Monday, January 24, 2011 1:53 AM
  • Yeah, sorry Ken ... I should have suggested ReadKey instead of Read. Here something to get you going:

    while (true)
    {
      ConsoleKeyInfo k = Console.ReadKey();
      if (k.Key == ConsoleKey.A)
        break;
    }
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 2:19 AM
    • Unmarked as answer by ken3663 Monday, January 24, 2011 2:20 AM
    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:27 AM
    Monday, January 24, 2011 2:15 AM
    Moderator
  • You don't need to add "true" in the for statement:

    for( ; ; )
    
    {
    
    	Console.WriteLine("looping...");
    
    
    
    	if (Console.KeyAvailable)
    
    	{
    
    		break;
    
    	}
    
    }
    
    

    If if remember well, there was a FOREVER macro defined just like that in the headers for my C compiler when I programmed on the Amiga.


    Yes, and a reason back in those days was that the compilers were rubbish, and a while(true) could generate an actual test, whereas for(;;) would not generate a test. (Another reason is readability - "FOREVER" may be marginally more readable than "for(;;)" or "while (true)").

    Nowadays, and in particular with C#, there is no need to do that for performance reasons; "for(;;)" and "while (true)" generate identical code. So now it comes down to personal preference.

    Strangely, I used to write for(;;) in C#, but in recent years I've come to prefer "while (true)".

    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:30 AM
    Monday, January 24, 2011 1:45 PM
  • Hi,

    Is there anything wrong with writting Eyal-Shilony loop in the following way:

     for (; !Console.KeyAvailable;)
    
    
    
         {
    
         Console.Beep(250, 600);
    
         Console.Beep(150, 600);
    
         }
    
    
    
        for (;true;)
    
    
    
        if (Console.KeyAvailable) break;
    
    

    ken3663


    Yes, there is something wrong: I think it's not very readable.

    I personally think that this would be better:

    while (!Console.KeyAvailable)
    {
        Console.Beep(250, 600);
        Console.Beep(150, 600);
    }

    Although either way, there's going to be a whole lotta beeping going on.

    • Marked as answer by ken3663 Wednesday, January 26, 2011 5:42 AM
    Tuesday, January 25, 2011 10:15 AM
  • Yes, there is something wrong: I think it's not very readable.

    and you're right. :)

    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    • Marked as answer by ken3663 Wednesday, January 26, 2011 5:42 AM
    Tuesday, January 25, 2011 3:47 PM
    Moderator

All replies

  • For the loop, use while instead of for:

    while (true)
    {
    }
    
    For checking for key presses, that depends on what your application. Is is a Console App (use Console.Read), is it a WinForm app (use an event handler for any of the key events: KeyDown, KeyPress, KeyUp), is it a WPF app (I don't do WPF, so I don't know what you'd use there).
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 1:40 AM
    Monday, January 24, 2011 1:30 AM
    Moderator
  •  for (int i = 0; i-1 < i++; i++)
          {
            MessageBox.Show("oh dear");
          }
    
    That worked for me.
    • Marked as answer by ken3663 Monday, January 24, 2011 1:42 AM
    Monday, January 24, 2011 1:30 AM
  • Hello,

    you can also do that like this.

    for (; !Console.KeyAvailable; )
    			{
    				Console.WriteLine("looping...");
    			}
    
    for (; true; )
    			{
    				Console.WriteLine("looping...");
    
    				if (Console.KeyAvailable)
    				{
    					break;
    				}
    			}
    


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    • Marked as answer by ken3663 Monday, January 24, 2011 2:21 AM
    Monday, January 24, 2011 1:46 AM
    Moderator
  • While Merlin's loop will work, I wouldn't recommend doing it that way for two reasons:

    1) It's not obvious that you are actually doing an infinite loop (and that you actually did mean to do an infinite loop).

    2) It's inefficient to be constantly incrementing i

    The while(true) syntax makes it clear that you actually did mean to do an infinite loop and it is quite efficient.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 2:01 AM
    Monday, January 24, 2011 1:47 AM
    Moderator
  • Hello Bonnie ,

    I'm new to programming. while (true) worked, thank you.

    This is a Console App and I tried Console.Read but I couldn't figure it out. If you have any other advice I would much appreciate it.

    Regards,

    Ken


    ken3663
    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:27 AM
    Monday, January 24, 2011 1:53 AM
  • Yeah, sorry Ken ... I should have suggested ReadKey instead of Read. Here something to get you going:

    while (true)
    {
      ConsoleKeyInfo k = Console.ReadKey();
      if (k.Key == ConsoleKey.A)
        break;
    }
    
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    • Marked as answer by ken3663 Monday, January 24, 2011 2:19 AM
    • Unmarked as answer by ken3663 Monday, January 24, 2011 2:20 AM
    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:27 AM
    Monday, January 24, 2011 2:15 AM
    Moderator
  • Thank you all for the help.

    ken


    ken3663
    Monday, January 24, 2011 2:34 AM
  • You don't need to add "true" in the for statement:

    for( ; ; )
    {
    	Console.WriteLine("looping...");
    
    	if (Console.KeyAvailable)
    	{
    		break;
    	}
    }

    If if remember well, there was a FOREVER macro defined just like that in the headers for my C compiler when I programmed on the Amiga.

    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:29 AM
    Monday, January 24, 2011 1:31 PM
  • You don't need to add "true" in the for statement:

    for( ; ; )
    
    {
    
    	Console.WriteLine("looping...");
    
    
    
    	if (Console.KeyAvailable)
    
    	{
    
    		break;
    
    	}
    
    }
    
    

    If if remember well, there was a FOREVER macro defined just like that in the headers for my C compiler when I programmed on the Amiga.


    Yes, and a reason back in those days was that the compilers were rubbish, and a while(true) could generate an actual test, whereas for(;;) would not generate a test. (Another reason is readability - "FOREVER" may be marginally more readable than "for(;;)" or "while (true)").

    Nowadays, and in particular with C#, there is no need to do that for performance reasons; "for(;;)" and "while (true)" generate identical code. So now it comes down to personal preference.

    Strangely, I used to write for(;;) in C#, but in recent years I've come to prefer "while (true)".

    • Marked as answer by ken3663 Tuesday, January 25, 2011 5:30 AM
    Monday, January 24, 2011 1:45 PM
  • Hey guys.

    I know that I didn't need to add 'true' there but I just wanted the OP to see that in comparison to the 'while(true)' loop, I probably should have mentioned it. :p

    Anyways, thank you for the clarification.


    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Monday, January 24, 2011 2:31 PM
    Moderator
  • Hi,

    Is there anything wrong with writting Eyal-Shilony loop in the following way:

     for (; !Console.KeyAvailable;)
    
              {
              Console.Beep(250, 600);
              Console.Beep(150, 600);
              }
    
            for (;true;)
    
            if (Console.KeyAvailable) break;
    

    ken3663
    Tuesday, January 25, 2011 6:21 AM
  • Hi,

    Is there anything wrong with writting Eyal-Shilony loop in the following way:

     for (; !Console.KeyAvailable;)
    
    
    
         {
    
         Console.Beep(250, 600);
    
         Console.Beep(150, 600);
    
         }
    
    
    
        for (;true;)
    
    
    
        if (Console.KeyAvailable) break;
    
    

    ken3663


    Yes, there is something wrong: I think it's not very readable.

    I personally think that this would be better:

    while (!Console.KeyAvailable)
    {
        Console.Beep(250, 600);
        Console.Beep(150, 600);
    }

    Although either way, there's going to be a whole lotta beeping going on.

    • Marked as answer by ken3663 Wednesday, January 26, 2011 5:42 AM
    Tuesday, January 25, 2011 10:15 AM
  • Yes, there is something wrong: I think it's not very readable.

    and you're right. :)

    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    • Marked as answer by ken3663 Wednesday, January 26, 2011 5:42 AM
    Tuesday, January 25, 2011 3:47 PM
    Moderator
  • Hmmm ... maybe it's just my opinion, but I don't think you should mark every post in this thread as an answer, Ken.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, January 26, 2011 5:50 AM
    Moderator
  • Hmmm ... maybe it's just my opinion, but I don't think you should mark every post in this thread as an answer, Ken.
    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Ok I won't, thanks for the heads up. Is this improper forum etiquette?
    ken3663
    Wednesday, January 26, 2011 6:02 AM
  • Well, I wouldn't say it's about etiquette but you should mark the few replies that were really helpful to you.

    Eyal, Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    SharpHighlighter - is an extension for Visual Studio, a fairly simple code highlighter for C#.
    Wednesday, January 26, 2011 8:30 AM
    Moderator
  • Hi Ken,

    Yes, as Eyal says, the purpose of marking a post as an answer is to let others know which posts helped you solve your problem. There's also the "Vote as Helpful" on the left side of each post if you'd like to recognize other posts as being helpful, but not actually the solution you went with.

    For example, there are often many ways to skin a cat, as they say. In this particular thread, you were shown several different syntaxes for doing the loop you asked about. Which one did you like best and end up using? Mark the post(s) that suggested that syntax as the answer. Perhaps other posts, offering different syntaxes were helpful, but you didn't end up using those other syntaxes. You could vote those as helpful, if you wish.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Wednesday, January 26, 2011 3:10 PM
    Moderator
  •  for (int i = 0; i-1 < i++; i++)
    {
    MessageBox.Show("oh dear");
    }
    That worked for me.


    Without the MessageBox it would be obvious that this does not actually create an infinite loop.  It is also much more complicated than necessary.

    Since int is a signed number (2s complement) it will wrap become negative at 0x80000000.  This code increments i by 2 each loop.  After around a billion iterations i will become 0x7ffffffe.  Since 0x7ffffffd < 0x7ffffffe it continues.  We then increment i to 0x7fffffff before getting to the heart of the loop.  After the loop i is incremented again to become 0x80000000.  We then check if 0x7fffffff < 0x80000000.  0x7fffffff is a positive number, 0x80000000 is a negative number and therefore the condition is not satisfied and we break out of the loop.

    If you set i=1 at the beginning it would have worked, although.

    The most obvious infinite loop is:

    infinite_loop:

    goto infinite_loop;

    Since goto is often seen as a bad coding practice (infinite loops are also often a bad idea) most people use

    while(true)

    -or-

    for(;;)

    some compilers generate warnings for while(true) so for(;;) may be the "best" practice.

    Another advantage of while or for over the goto is that you can break out of a for or while loop if the correct conditions are satisfied.  If you ever needed to get out of a goto loop then you would need to use another goto.

    Friday, April 01, 2011 4:32 PM
  • take global bool value as true

     

    make it false when with keypress event fired on winforms

     

    then use while(bookvalue){}


    Friday, April 01, 2011 6:43 PM