Thread: for loop not terminating

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    for loop not terminating

    Code:
    for (i = insertionPoint; i != start; i--)
    {
    	if (i < 0)
    		i += BUF_SIZE;
    
    	cout << "swapping " << i << " and " << start << endl;
    
    	swap (buffer, i, start);
    
    	if (i == start)
    		cout << "I really should be terminating under this condition." << endl;
    }
    This just goes on and on despite reporting the terminating condition in the body of the loop.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Its because i decrements THEN it checks the loop condition. So if (i==start) it'll print your statement, then it decrements by one, then it checks again but now i doesn't equal start anymore so the loop continues.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Hi

    dont u need to use the "break" to terminate a loop

    e.g

    Code:
     	
    Code:
    
    for (i = insertionPoint; i != start; i--)
    {
    	if (i < 0)
    		i += BUF_SIZE;
    
    	cout << "swapping " << i << " and " << start << endl;
    
    	swap (buffer, i, start);
    
    	if (i == start)
    		cout << "I really should be terminating under this condition.";
                    break;
    }

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    1) --i would decrement then check. i-- will check then decrement.

    2) I was tempted to just stick a break in there, but I really shouldn't have to if the compiler does its job correctly.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  5. #5
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    i dont think a break would hinder the program at all

    maybe add a couple of milliseconds on the processing time

    does the addition of the break sort it out?

    cheers

    Alex

  6. #6
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Using break statements is generally considered undesirable from a design standpoint.

  7. #7
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Quote Originally Posted by joshdick
    Using break statements is generally considered undesirable from a design standpoint.
    can u please explain further?

    Cheers

  8. #8
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Quote Originally Posted by joshdick
    Using break statements is generally considered undesirable from a design standpoint.
    I must admit I use them a bit. But here the terminating condition is clear: when the 2 values become one and the same. The for statement should sort it.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  9. #9
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    break statements cause the flow of your program to jump suddenly from one place to another like goto, and gotos are considered really bad. Code that uses a lot of gotos and breaks is often called "spaghetti code" because it becomes very hard to follow.

    It is very important to write code that others can follow easily, so break and goto should often be avoided. I'm not saying they should never be used, and I certainly don't want another stupid flamewar over this. But I will say that in the majority of times, there's a better way to code a loop like this than with a bunch of break statements in it.

    I should also point out that breaks are sometimes required like in a switch, and that's just peachy.

  10. #10
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by samGwilliam
    This just goes on and on despite reporting the terminating condition in the body of the loop.
    Post more code. I tend to think it's because of the i += BUF_SIZE. This works fine:
    Code:
    const int BUF_SIZE = 100;
    
    int main(void)
    {
    	int insertionPoint = 5;
    	int start = 1;
    	for (int i = insertionPoint; i != start; i--)
    	{
    		if (i < 0)
    			i += BUF_SIZE;
    
    		std::cout << "swapping " << i << " and " << start << std::endl;
    
    	//	swap (buffer, i, start);
    
    		if (i == start)
    			std::cout << "I really should be terminating under this condition." << std::endl;
    	}
    	return 0;
    }
    Output:
    Code:
    swapping 5 and 1
    swapping 4 and 1
    swapping 3 and 1
    swapping 2 and 1
    Quote Originally Posted by PJYelton
    Its because i decrements THEN it checks the loop condition. So if (i==start)...
    If i == start, then you shouldn't have gone through the loop that time.
    Quote Originally Posted by samGwilliam
    1) --i would decrement then check. i-- will check then decrement.
    Actually, both versions would decrement then check since the --i is in execution section of the for loop, and the check is in the conditional section of the for loop.
    Quote Originally Posted by samGwilliam
    2) I was tempted to just stick a break in there, but I really shouldn't have to if the compiler does its job correctly.
    In a contest of wills, the compiler will usually win. Hence, it's our job as programmers to program so that the compiler will understand.

    Like I said, post more code.
    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

  11. #11
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    kk..cool

    so the break implimentation in my code "the cout thread"
    ..is still ok?
    or is it the start of "spaghetti code!"

    Cheers

  12. #12
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    >>Actually, both versions would decrement then check since the --i is in execution section, and the check is in the conditional section.<<

    Beat me to the punch

  13. #13
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Quote Originally Posted by macman
    so the break implimentation in my code "the cout thread"
    ..is still ok?
    or is it the start of "spaghetti code!"
    It's usually better to re-write the loop.
    Code:
    while(choice != 'q') {
    	do {
    	cout << "Help On: \n";
    	cout << "  1. if\n";
    	cout << "  2. switch\n";
    	cout << "  3. for\n";
    	cout << "  4. while\n";
    	cout << "  5. do=while\n";
    	cout << "  6. break\n";
    	cout << "  7. continue\n";
    	cout << "  8. goto\n";
    	cout << "===================\n";
    	cout << "Choose One (q to quit): ";
    	cin >> choice;
    	} while( choice < '1' || choice > '8' && choice != 'q');
    }
    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

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    39
    Thanx for the revamped code pianorain!

    Ill keep this in mind..its good to start of nice habbits!
    Cheers

    And samGwilliam, im sorry if ive sort of swamped your thread...

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    82
    Quote Originally Posted by macman
    kk..cool

    so the break implimentation in my code "the cout thread"
    ..is still ok?
    or is it the start of "spaghetti code!"

    Cheers
    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.

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