CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to use 'break' jump out of 3 nested loop?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cchappyboy



Joined: 03 Dec 2008
Posts: 45

View user's profile Send private message

How to use 'break' jump out of 3 nested loop?
PostPosted: Wed Jun 01, 2011 9:28 am     Reply with quote

Hi

I want to use 'break' jump out of loops. The code in below.
Code:

while(condition1)
{
   while(condition2)
   {
      while(condition3)
      {
           if(condition)
           {
               break;
           }
      }//loop3
         dosomething3();
    }//loop2
         dosomething2();
}//loop1
dosomething1();


So after break is happened, which line is going to be executed.
Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19510

View user's profile Send private message

PostPosted: Wed Jun 01, 2011 9:34 am     Reply with quote

'break', always exits the innermost loop only.
What will be executed, will depend on what condition2, and condiftion1 evaluate to.
Normal method:
Code:

int1 exit=FALSE;

while(condition1 && (exit==FALSE))
{
   while(condition2 && (exit==FALSE))
   {
      while(condition3 && (exit==FALSE))
      {
           if(condition)
           {
               exit=TRUE;
           }
      }//loop3
         dosomething3();
    }//loop2
         dosomething2();
}//loop1
dosomething1();

This way you can design your code to exit as many levels as you want.

Best Wishes
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Wed Jun 01, 2011 12:27 pm     Reply with quote

If you don't apply for a good coding style award, you'll notice, that a simple goto will save both text lines and code space. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group