Thread: continue in while loops

  1. #1
    Unregistered
    Guest

    continue in while loops

    hello again folks,

    does anyone know how i can have my continue loop back to a previously made while loop. what i mean is this folks:

    while loop 1()
    while loop 2()
    continue;

    how can i have continue direct the program to the while loop 1?


    tnx,
    ben

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    19
    If you want to get out of the loop you should use break.

  3. #3
    Unregistered
    Guest

    Exclamation no, i mean...

    nononono, i dont want to get out of the loop. well in a sense i am getting out, its just that i want continue to get back to the first while loop in order to execute the loop once again.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This sounds like an excellent case of bad design. But assuming the second loop isn't nested in the first then you could use a variation of continue called goto. But before relying on goto, consider your algorithm and how you can improve it because generally, when you use a local jump in your code it's because you didn't think the design through well enough.
    Code:
    loop1:
      while ( foo ) {
        .
        .
      }
      while ( bar ) {
        .
        .
      }
      if ( flag == DO_OVER )
        goto loop1;
    I fully expect some of the anti-goto people to scream in horror at this point. "No! How can Prelude suggest the use of goto?! I thought she knew it was evil!", just note before making such a post that I'll laugh heartily before responding appropriately

    -Prelude
    My best code is written with the delete key.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a better idea:

    Code:
    do switch( choice )
    {
        case CHOICE_LOOP_ONE: function1( ); break;
        case CHOICE_LOOP_TWO: function2( ); break;
    } while( choice != CHOICE_BREAK );
    No need for a goto.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >No need for a goto.
    Exactly, there's almost never a need for goto. The problem is in the design. However, to solve that particular problem without changing the design of the code you'd need goto...but as I said, that's a bad design.

    -Prelude
    My best code is written with the delete key.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Oooh! Here we go:

    Make function1 and function2 recursive, and so they call eachother! Something like:
    Code:
    function1
    {
        do choice
            if choice == 1 function1( )
            if choice == 2 function2( )
            else exit(0);
    }


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Hehe, and then you get to complain about how everyone who took you seriously writes bad code, right?

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-03-2008, 09:48 PM
  2. Continue and switch
    By camzio in forum C Programming
    Replies: 10
    Last Post: 10-04-2008, 08:31 AM
  3. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  4. switch - continue
    By DavidP in forum C Programming
    Replies: 2
    Last Post: 06-24-2004, 10:09 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM