break question

This is a discussion on break question within the C Programming forums, part of the General Programming Boards category; Hi all, I have the following question, f.e. I am currently using two while loops, one inside the other, now, ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    19

    break question

    Hi all, I have the following question, f.e. I am currently using two while loops, one inside the other, now, if a certain condition is met, I want BOTH the while loops to be terminated using break;, unfortunately, it seems only one break; is being done, now, my question is, how can I solve this in a GOOD way (not amateur stuff, I want to keep my code clean)

    example:

    Code:
    while(1) {
      while(1) {  
         if(condition) {
           break;
           break;
         }
      }
    }

  2. #2
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Make one of the while conditions false when you break from it.

    Code:
    a=1;
    while(!a)
     while(!a)
      if(b == 2)
       a=0;
    The most efficient way though, is to put your loop into a function, and use a return value whenever you want to break.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    try

    Code:
        while(1) {
            while(1) {  
                if(condition) break;
            }
            break;
        }

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What, no shout out for goto?
    Code:
    while( foo )
    {
        while( bar )
        {
            if( baz )
                goto foobarbaz;
        }
    }
    
    foobarbaz:



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

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    19
    thnx, I thought it may could have been solved without adding the extra variable, I'll try that other method another time, once my code is finished, the thing you suggested will do for now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 01:22 PM
  3. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 01:11 PM
  4. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 06:23 AM

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