Thread: for loop of evil

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    for loop of evil

    is this loop :

    for(;
    illegal?

    because I used it in one of my programs and it works fine, I think it is called and INFINATE LOOP, anyway, one of my programming books said it was bad practice to use these types of FOR loops..
    but gave no reason.

    Do you guys know?

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    As far as I know, it should just be an infinite loop. (So legal).

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    I presume you mean "for (;;)", you can disable smilies in your post if you choose to and make this sort of stuff easier to read.

    There is nothing wrong with it, it is exactly the same as "while (true)" or "while (1)" and what you choose is up to standards and personal preference. It is unlikely in today's compilers that the actual machine instructions differ amongst the variations, but in the old days there may have been a difference and on certain compilers one would be more efficient than the others.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    Quote Originally Posted by swgh
    is this loop :

    for(;;)
    illegal?

    because I used it in one of my programs and it works fine, I think it is called and INFINATE LOOP, anyway, one of my programming books said it was bad practice to use these types of FOR loops..
    but gave no reason.

    Do you guys know?
    Like the others have said, it's perfectly legal.

    The "bad practice" is when you use an infinite loop, but it has a non-error terminating condition. Something like:
    Code:
      char c;
      using std::cin;
    
      for (;;) {
        cin.get(c);
        if ( !cin )                  // terminating condition
          break;
        ...
      }
    The loop is "advertised" as infinite (no condition test), but there is in fact one. Namely, the end of input. So the "better practice" would be to use a different loop, and put the condition test where it should be:

    Code:
      char c;
    
      while ( std::cin.get(c) ) {
        ...
      }
    Of course, this is personal opinion. Some programmers will see no difference here at all, and might be offended if someone suggested that it was somehow "bad". Other programmers will see the difference, and might be offended that you wouldn't take the time to do it the "right way".

    My opinion is simply to say what you mean. If the loop should never terminate, then use an infinite loop. If there is a terminating condition, put it where it's supposed to go. Lastly, don't be offended (wink, smile).

    HTH,
    Will

  5. #5
    Learn from the llama Marlon's Avatar
    Join Date
    Jun 2005
    Location
    South africa
    Posts
    25
    Just like the others said for(; is infinite cause there is now terminating condition
    `Who are YOU?' said the Caterpillar.
    This was not an encouraging opening for a conversation. Alice replied, rather shyly, `I--I hardly know, sir, just at present-- at least I know who I WAS when I got up this morning, but I think I must have been changed several times since then.' - Lewis Caroll's Alice in Wonderland.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by whoie
    The "bad practice" is when you use an infinite loop, but it has a non-error terminating condition.
    I disagree. If the only condition under which the application should exit is when the system hardware dies or is powered down, there is often no need for any terminating condition. In such a case (depending on hardware design) there is not necessarily any means for the software to detect a failure, so it can not choose to exit in response.

  7. #7
    Registered User
    Join Date
    Jul 2003
    Posts
    110
    I disagree with your disagreement, because that's what I was trying to say. Thank you for the clarification. :-)

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I find that infinite loops add a lot of readability in many situations, where the loop may exit at many different points. I find it distasteful to go through the hassle of ensuring that the code following an 'should exit' point will have no negative side-effects if you wait for the loop to return to the beginning before breaking (also a HUGE pain when debugging, having to run through it all again and ensure that you got it right the first time), or worse, having another layer of nested 'if' for each consecutive exit point, or having to run "if(!exit)" checks in front of everything. If you use an infinite loop and break at the appropriate points, you ensure immediately that the subsequent code is not executed, which is also nice for debugging - it saves you several (dozen) steps as you walk through an insufferably complex breaking mechanism that relies exclusively on the test condition. It is, of course, also desirable for efficiency reasons; it can save you a good number of branching statements. Although, of course, infinite loops aren't to be used indiscriminately either
    Last edited by Hunter2; 06-23-2005 at 04:05 PM.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone a Resident Evil fan?
    By SlyMaelstrom in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-02-2005, 08:21 PM
  2. Resident Evil: The Evil Begins-Coder Needed
    By ^Tomala^ in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 01-04-2005, 05:37 PM
  3. Evil Fish
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 29
    Last Post: 07-10-2003, 10:27 AM
  4. evil evil empirical authority....
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 03-25-2003, 04:33 PM
  5. President Evil
    By GanglyLamb in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 01-29-2003, 07:54 AM