"break" - quick response needed

This is a discussion on "break" - quick response needed within the C Programming forums, part of the General Programming Boards category; can someone tell me which loop i'll break out of and where i'll end up? for (I=0; I < spaceCounter; ...

  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
    Australia
    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
    Australia
    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 04:04 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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: 09-01-2004, 12:24 AM
  5. error with function, help needed quick
    By chris285 in forum C++ Programming
    Replies: 3
    Last Post: 04-30-2003, 09:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21