Thread: "break" - quick response needed

  1. #1
    luke
    Guest

    "break" - quick response needed

    can someone tell me which loop i'll break out of and where i'll end up?

    for (I=0; I < spaceCounter; I++)
    {
    while (ptr2 != ptr1)
    {
    if (*ptr2 == ' ')
    {
    if (alternate == 1)
    {
    while (*ptr2 == ' ')
    {
    *ptr3++ = *ptr2++;
    }
    *ptr3++ = ' ';
    alternate = 0;
    break;
    }
    else
    {
    while (*ptr2 == ' ')
    {
    *ptr3++ = *ptr2++;
    }
    alternate = 1;
    }
    }
    else
    {
    *ptr3++ = *ptr2++;
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Code:
    for (I=0; I < spaceCounter; I++) { 
        while (ptr2 != ptr1) { /*Breaks from this loop here*/
            if (*ptr2 == ' ') { 
                if (alternate == 1) { 
                    while (*ptr2 == ' ') { 
                        *ptr3++ = *ptr2++; 
                    } 
                    *ptr3++ = ' '; 
                    alternate = 0; 
                    break; 
                } 
                else  { 
                    while (*ptr2 == ' ') { 
                        *ptr3++ = *ptr2++; 
                    } 
                    alternate = 1; 
                } 
            } 
            else { 
                *ptr3++ = *ptr2++; 
            } 
        } 
    }
    if you layout your code PROPERLY, then you should see that it is the second loop

    kwigibo

  3. #3
    Luke
    Guest
    thanks for the reply, so do you mean the "while" loop?


    ps
    (i did lay it out properly, but it came out ****ed up after i posted it)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Location
    Fiji
    Posts
    212
    Fair enough, forgiven then. Next time put the code in tags [ code ] to open and [ /code ] to close. don't use tabs too. use spaces, it's a pain in the butt, i know.

    and yes it is the while loop.


    kwigibo
    Last edited by kwigibo; 04-23-2002 at 03:04 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can someone tell me which loop i'll break out of and where i'll end up?
    When you break from a nested loop, the break will only occur on the loop that you call it from. So if you have three loops nested inside each other, a break from the inner most loop will take you to the point in the second loop right after the inner loop.
    Code:
    while ( something ) {
      while ( somethingElse ) {
        while ( anotherSomething ) {
          break;
        }
        /* You end up here */
      }
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick help needed...
    By AssistMe in forum C Programming
    Replies: 4
    Last Post: 03-02-2005, 12:03 PM
  2. Quick help needed....!!
    By AssistMe in forum C Programming
    Replies: 1
    Last Post: 02-25-2005, 05:44 AM
  3. Some quick help needed
    By disco_dog in forum C Programming
    Replies: 11
    Last Post: 12-06-2004, 09:37 AM
  4. C++ help needed
    By Enkindu in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 08-31-2004, 11:24 PM
  5. error with function, help needed quick
    By chris285 in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 08:31 AM