Thread: for loop not terminating

  1. #16
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >1) --i would decrement then check. i-- will check then decrement.
    Not as the third step of a for loop. First there's the initialization, then the condition is treated as a top-of-loop test, the body is performed if the condition is true, then the increment is performed, and the condition is tested again.

    >Using break statements is generally considered undesirable from a design standpoint.
    And from an implementation standpoint, using break statements usually results in cleaner code. It would be nice if practice were like theory, but in practice, theory tends to fall flat. If you can fiddle with the condition to avoid a break, more power to you. However, if you need to use flags and conditionals or use an uncommon idiom to avoid a break, it's probably not worth the added complexity.

    >This just goes on and on despite reporting the terminating condition in the body of the loop.
    Maybe if you weren't modifying i in the body of your loop, the problem would be easier for you to see. It's generally a bad idea to write to the counter in a for loop because it changes how many times the loop iterates. It's harder to verify correctness that way.
    My best code is written with the delete key.

  2. #17
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Normally I would use "i >= start" but this is a circular buffer (hence the wrap) so it's not about whether it's bigger or smaller. Both are valid configurations. However, as soon as the two meet the swapping can finish.

    I put a break in and it works just as expected. But I should NOT have to do this.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #18
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by AH_Tze
    It's unnecessary. Simply be looking at the control statement you should be able to understand what variables control the loop, and what conditions the loop continues/stop execution.

    There are sometimes cases when there is other information that needs to be taken into account (ie step through a container and do something to it's data, BUT if something wierd happens (like a pointer that should have data is null) break.)

    It doesn't look like this is the case. He just wants to step through data until it's done, which given a start and end point should be possible with just the for-loop. I don't quite understand that what the BUF_SIZE is for though. Seems like a likely spot for a logic error.
    #define BUF_SIZE 10

    BUF_SIZE is the total size of the array. When decrementing it will check to see if the index has gone negative - if it has then add BUF_SIZE (10) to it. This will result in 9 for -1, which is indeed the last slot in the array.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  4. #19
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Couldn't you just mod it by BUF_SIZE?

  5. #20
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by joshdick
    Couldn't you just mod it by BUF_SIZE?
    If it was incrementing then sure thing but since when did -1 % 10 = 9?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  6. #21
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Try this:
    Code:
    	for (int i = insertionPoint; i != start; i-1 < 0 ? i += (BUF_SIZE - 1) : i--)
    It works, but I'm still trying to sort out exactly why.

    edit: When start = BUF_SIZE - 1, the previous for loop would not stop because i would equal -1 on its pass through the loop. You have to get the BUF_SIZE - 1 into the conditional for it to work.
    Last edited by pianorain; 04-07-2005 at 02:08 PM.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Terminating Zero
    By DickArmy in forum C Programming
    Replies: 9
    Last Post: 07-01-2009, 01:53 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. error: missing terminating " character
    By nasim751 in forum C Programming
    Replies: 2
    Last Post: 04-17-2008, 01:50 AM
  4. terminating a while loop with a character
    By just_learning in forum C Programming
    Replies: 3
    Last Post: 04-13-2007, 05:22 AM
  5. terminating wrong process
    By Queatrix in forum Windows Programming
    Replies: 12
    Last Post: 09-09-2006, 01:16 AM