Thread: Break out of two loops

  1. #1
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17

    Break out of two loops

    Hello, I would like to break out of the loop i am in so i would put break;. My problem is i want to be able to do it inside of a switch so the break breaks out of the switch not the loop. Is there a way i can break out of both of them.
    Thanks.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is there a way i can break out of both of them.
    Not an elegant way. The two most common ways are a goto and placing the entire construct inside of a function so that you can use return.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    13
    Well depending on how your loop is set up (ie what your sentinal value is) you can just set a variable to the sentinal for example when using the for loop:
    Code:
    for (int i = 0; i < 10; i++)
    just set i to 10

    if your using a while loop just create a state that doesnt evaluate to true in the while clause for example:
    Code:
    bool done = false;
    while(!done)
    {
       switch(my_var)
        {
         case 1:
             func1();
         break;
         case 2:
             func2();
             done = true;
         break;
         defualt:
            cerr << "\a Illegal State!\n";
         break;
       }
     }
    Hope this helps!
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  4. #4
    Registered User MiLo's Avatar
    Join Date
    Jun 2004
    Posts
    17
    Thanks ZAK. That worked really well. Not sure why I didn't think of that:P. Thanks.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    bool done = false;
    There is no bool type in C. In C99 there is _Bool (stdbool.h) however. But the concept is the same, or similar:

    Code:
    for( foo = 0; foo < bar && condition == keepgoing; foo++ )
    {
        for( baz = 0; baz < bongo; baz++ )
        {
            if( somecondition )
            {
                condition = !keepgoing;
                break;
            }
        }
    }
    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    13
    good call quzah! I actually posted a C++ solution to a C question...you also can not declare a variable in side of a loop in C. I would however not put a flag in the loop when you can just use the sentinal of the loop in your case bar, but you would trade off some readability. Another good way to define bool is a common way that was done in C++ before bool was part of alot of compilers (remember VC++ 1.0) which is equivalent in C.
    Code:
    typdef char bool
    #define TRUE 1;
    #define FALSE 0;
    Thanks again for correcting my errant code!
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  7. #7
    ---
    Join Date
    May 2004
    Posts
    1,379
    prelude told me to use this
    Code:
    typedef enum { false, true } bool;

  8. #8
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Prelude's bool is lot more elegant IMHO.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Elegant it may be, but it can also be a trap.

    bool matched = strcmp(word1,word2) == 0;

    Where types are more strictly enforced, this generates warnings.
    The left is an enumeration, the right is an int.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    May 2002
    Posts
    13
    This is true Salem, but strcmp never was designed to return a boolean it returns an integer... it really isn't a boolean function. The enumeration code is a better elegant solution to the problem.
    "Computer Science is no more about computers than astronomy is about telescopes"
    - E.W. Dijkstra

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but strcmp never was designed to return a boolean it returns an integer... it really isn't a boolean function.
    No, strcmp doesn't return a boolean value, but an expression that uses it with a relational operator does. However, the result is still the same type, int. The problem is the mismatch between integral and enumerated types. The standard actually allows it AFAIK, but mentions potential traps in the Common Warnings section.

    >prelude told me to use this
    I did? Maybe it was mentioned as an alternative or shown in example code, I typically don't recommend one option over the other for something like this, and I certainly don't recommend redefining standard names without very good cause. My example probably looked more like this:
    Code:
    typedef enum {BFALSE, BTRUE} bool_t;
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  3. Keypress reading
    By geek@02 in forum Windows Programming
    Replies: 1
    Last Post: 06-16-2004, 12:16 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM