|
-
May 21st, 2009, 01:31 PM
#1
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?
-
May 21st, 2009, 01:35 PM
#2
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;
}
}
-
May 21st, 2009, 01:37 PM
#3
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
-
May 21st, 2009, 01:42 PM
#4
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
}
-
May 21st, 2009, 01:56 PM
#5
Re: loop inside a loop, how to break; both?
code above freezes my program
-
May 21st, 2009, 02:08 PM
#6
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
-
May 21st, 2009, 02:46 PM
#7
Re: loop inside a loop, how to break; both?
 Originally Posted by Owyn
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?
-
May 21st, 2009, 08:13 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|