CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2009
    Posts
    140

    Post loop inside a loop, how to break; both?

    Code:
    for (int i = 0; i < 10; i++)
    { 
    
    
    for (int h = 0; h < 5; h++)
    		{ 
    			break;
    		{ 
    
    
    
    
    }
    my break; is working only on outside loop [i], but inside loop [h] runs 5 times everytime, if i put break;break; - nothing changes at all O_o

    how do i break inside loop?

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: loop inside a loop, how to break; both?

    You can use a bool flag.
    Code:
    bool breakAll = false;
    for (int i = 0; i < 10 && !breakAll; i++)
    { 
      for (int h = 0; h < 5 && !breakAll; h++)
      { 
        breakAll = true;
      }
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    May 2009
    Posts
    140

    Re: loop inside a loop, how to break; both?

    actually my break; is continue; i want to skip to next, i just used break; cuz continue; continued on the same check forever

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: loop inside a loop, how to break; both?

    Then why don't you say so on the first place? If you want to buy apples you don't ask for peaches.
    Code:
    for (int i = 0; i < 10; i++)
    { 
      bool skipTheRest = false;
      for (int h = 0; h < 5; h++)
      { 
          if(...)
          {
             skipTheRest = true;
             break; // from the inner if
          }
      }
    
      if(skipTheRest)
         continue;
    
      // the rest
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    May 2009
    Posts
    140

    Re: loop inside a loop, how to break; both?

    code above freezes my program

  6. #6
    Join Date
    May 2009
    Posts
    140

    Re: loop inside a loop, how to break; both?

    found a solution, break inside 2nd loop was working for 2nd loop, if 2nd loop's step had no break it continued forever

    i added a bool condition for 2nd loop conditions, break for every not met if condition in 2nd loop, and check for bool to do else action in 1st loop after 2nd, but without any break in 2nd loop and it works like it should now

  7. #7
    Join Date
    Apr 2008
    Posts
    725

    Re: loop inside a loop, how to break; both?

    Quote Originally Posted by Owyn View Post
    i added a bool condition for 2nd loop conditions, break for every not met if condition in 2nd loop, and check for bool to do else action in 1st loop after 2nd, but without any break in 2nd loop and it works like it should now
    lolwut?

  8. #8
    Join Date
    Nov 2006
    Posts
    1,611

    Re: loop inside a loop, how to break; both?

    That might also go like this:

    Code:
    inline bool interior()
    {
      for (int h = 0; h < 5; h++)
      { 
          if(...)
          {
             return false;
          }
      }
    
     return true;
    }
    
    
    for (int i = 0; i < 10; i++)
    { 
      if( ! interior() ) continue;
    
      // the rest
    }
    
    
    or that could be
    
    
    for (int i = 0; i < 10; i++)
    { 
      if( interior() ) 
        {
         // the rest
        }
    }

    If common data is used in the interior and the i loop, these could be the member of a class, which holds the common data.
    If my post was interesting or helpful, perhaps you would consider clicking the 'rate this post' to let me know (middle icon of the group in the upper right of the post).

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured