Thread: breaking a loop

  1. #1
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163

    Question breaking a loop

    Sorry for the simple question -

    If i am in two loops and I put a break in the middle does it stop the loop it is in or does it stop all loops?

    Code:
    for(i=0; i<10; i++)
    {
           for(j=0; j<10; j++)
          {
              if(example[i][j]>100) {break;}
          }
    }
    My site to register for all my other websites!
    'Clifton Bazaar'

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Just the inner-most loop.

    If you want to exit BOTH loops, then put the double looping into it's own function and use return. Another alternative is to use goto BUT DONT DO THAT OR ELSE I WILL KILL YOU.

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Better to design your loops with the exit routes already planned...

    Code:
    Continue = TRUE;
    i = -1;
    J = -1;
    
    while (Continue && (i < 10))
    {
        ++i;
        while (Continue && (j < 10))
        {
            ++j;
            if (example [i][j] > 100) Continue = FALSE;
        }
    }
    ... or similar.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    You generally don't want to do that because it forces an extra check every single time you go through your loops and adds another variable.

  5. #5
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    thanks all for the help

    Another alternative is to use goto BUT DONT DO THAT OR ELSE I WILL KILL YOU.
    I remember programming in 'basic' and using GOTO, nowdays everyone threatens to kill you if you use it
    My site to register for all my other websites!
    'Clifton Bazaar'

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    >>>
    You generally don't want to do that because it forces an extra check every single time you go through your loops and adds another variable.
    <<<

    The check on a simple boolean is very fast, and I like to have my escape routes planned at the design stage, easy to modify later. The variable would be a stack variable so I don't really care about that either.

    In any case, I expect the optimized code generated would be similar.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    adrianxw is right. break, return (in the middle of a function), goto are similar in that they break out of a smooth flowing function. They "jump" in an assembly like way and not a high level language like way.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets and breaking out of while loop
    By pollypocket4eva in forum C Programming
    Replies: 25
    Last Post: 01-05-2009, 06:50 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. A somewhat bizzare problem!!! - WHILE LOOP
    By bobthebullet990 in forum C Programming
    Replies: 3
    Last Post: 03-31-2006, 07:19 AM
  4. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 03:45 PM
  5. Breaking out of a loop
    By escarrina in forum C Programming
    Replies: 9
    Last Post: 04-05-2004, 12:05 PM