View previous topic :: View next topic |
Author |
Message |
cchappyboy
Joined: 03 Dec 2008 Posts: 45
|
How to use 'break' jump out of 3 nested loop? |
Posted: Wed Jun 01, 2011 9:28 am |
|
|
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: 19819
|
|
Posted: Wed Jun 01, 2011 9:34 am |
|
|
'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
|
|
Posted: Wed Jun 01, 2011 12:27 pm |
|
|
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.  |
|
 |
|