Reputation: 9319
If I use break
like the code below, then the loop within row
won't iterate the rest if there is a match in the beginning, but what about the col
loop?
Would it still iterate between 0 and 7? Is there a way to use break
there as well?
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
//Then do this;
break;
}
Upvotes: 37
Views: 20394
Reputation: 1
We could use the concept of a flag variable:
flag = 1;
for (int col = 0; col < 8; col ++)
{
if (flag == 1)
{
for (int row = 0; row < 8; row ++)
{
if (flag == 1)
{
if (check something)
{
//Then do this;
flag = 0;
}
}
}
}
}
Upvotes: -1
Reputation: 163
Loop1:
for (int col = 0; col < 8; col ++)
{
for (int row = 0; row < 8; row ++)
{
if (condition)
{
break Loop1;
}
}
}
This could do what you need.
Upvotes: 0
Reputation: 1459
I think you should use a tag or a label, like "outerLoop". This works in Java:
outerLoop:
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
//Then do this;
break outerLoop;
}
Upvotes: 1
Reputation: 143037
In Java, you can use a break label
.
outer:
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
break outer;
}
}
}
And, since nobody else has mentioned it yet, in C#, you can use goto label
.
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
goto outside;
}
}
}
outside:
Upvotes: 3
Reputation: 1503954
One option is to use a condition flag. You could then either break in the outer loop as well, or just use it as an extra condition within the for
loops:
bool keepGoing = true;
for (int col = 0; col < 8 && keepGoing; col++)
{
for (int row = 0; row < 8 && keepGoing; row++)
{
if (something)
{
// Do whatever
keepGoing = false;
}
}
}
In Java, you can specify a label to break to though. (I didn't see that this question was tagged Java as well as C#.)
outerLoop:
for (...)
{
for (...)
{
if (...)
{
break outerLoop;
}
}
}
EDIT: As noted in comments, in C#, you could use a label and goto
:
for (...)
{
for (...)
{
if (...)
{
goto endOfLoop;
}
}
}
endOfLoop:
// Other code
I'd really recommend that you don't take either of these approaches though.
In both languages, it would usually be best to simply turn both loops into a single method - then you can just return from the method:
public void doSomethingToFirstOccurrence()
{
for (...)
{
for (...)
{
if (...)
{
return;
}
}
}
}
Upvotes: 90
Reputation: 66649
One more alternative to the other answers is to set your counters to the maximum, to stop the loops.
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something)
{
// Use the col and row here.
// Now we go for a totally break of all loops.
// To stop the loops you can set that to the maximum
// of your loop test.
row = 8;
col = 8;
}
The advantage to that trick is that you do not add any additional checking code to the full loop and that makes it a lot faster.
Upvotes: 2
Reputation: 4393
nameHere:
for (yourForLoop) {
for (anotherLoop) {
if(condition) {
break nameHere;
}
}
}
Upvotes: 3
Reputation: 2064
Yes, it is possible by using a break
label:
package others;
public class A {
public static void main(String[] args) {
outer: for(int col = 0; col < 8; col ++)
{
for (int row = 0; row < 8; row ++)
{
if (col == 4)
{
System.out.println("hi");
break outer;
}
}
}
}
}
Upvotes: 15
Reputation: 2385
It doesn't exit the col loop.
Instead, you can wrap all in a function and use return;
to exit immediately from the loop
private Xy Loop( /* Parameters */)
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something) {
// Then do this;
return something; //Or just return;
}
}
}
}
Upvotes: 3
Reputation: 41240
You can put logic like this:
boolean condition = false;
for (int col = 0; col < 8; col ++)
for (int row = 0; row < 8; row ++)
if (check something) {
// Then do this:
condition = true; // Break condition for outer loop
break;
}
}
if (condition)
break;
}
Upvotes: 6
Reputation: 2644
break
only breaks the loop that is directly around it. You could use a flag to control the outer loop:
boolean continueOuterLoop = true;
for(int col = 0; continueOuterLoop && col < 8; col ++) {
for(int row = 0; row < 8; row ++) {
if(check something) {
//Then do this;
continueOuterLoop = false;
break;
}
}
}
Upvotes: 3
Reputation: 1012
There are a few ways to do this. One way is to set the max value of the variable in the outer loop.
int maxcol = 8;
for (int col = 0; col < maxcol; col++)
{
for (int row = 0; row < 8; row++)
{
if (check something)
{
Then do this;
// cause the outer loop to break:
col = maxcol;
// break the inner loop
break;
}
}
}
Upvotes: 0