Reputation: 797
Can someone tell me how to break the main loop when I have nested loops?
Example*:
/*Main loop*/
for(int y = 0; y < 100; y+=10)
{
/*Sub loop*/
for (int x = 0; x < 100; x += 10)
{
if(x == 60)
{
//Break the main loop
}
}
}
*This code do nothing, it's just an example
What should I put in the place of the "Break main loop" comment? In java there are labels which I can break (when i set a label to the main loop named "MainLoop" I can write "break MainLoop;" and it will be valid), but what can I do here?
Thanks in advise!
Upvotes: 10
Views: 5319
Reputation: 121702
goto
!
I fail to understand this persistent meme which says that goto
is considered "harmful". When used correctly, it is very powerful, and this is such a case.
Upvotes: 19
Reputation: 2872
I used LINQ to gather the interesting objects first then performed the operations on the results of the LINQ query. Thereby removing nested loops and replacing with one loop.
Upvotes: 0
Reputation: 463
As others have said, the "correct" answer depends on the problem you're solving. If you can, breaking it into smaller pieces is the preferred route. Something along this model:
object MainLoop ()
{
object result = null;
for(int y = 0; y < 100; y+=10)
{
result = SubLoop(y);
if (result != null)
{
break;
}
}
return result;
}
object SubLoop (int y)
{
object result = null;
for (int x = 0; x < 100; x += 10)
{
if(x == 56)
{
result = objectInstance;
break;
}
}
return result;
}
In my opinion, it's ugly to varying degrees to have multiple return statements from a single function, use extra flags, or (shudder) use goto's. But, sometimes one of those is necessary.
Edit: This is demonstrating using this method to return a useful object of some kind, you would have "Customer" or "IDataAccess" or "bool" or something other than "object" as the return types if using this for real.
Upvotes: 0
Reputation: 108790
return
is often possible by putting the loops into a seperate function.goto
.Upvotes: 11
Reputation: 91600
Since, as you've mentioned, there's no labels on the break
command, you could do something like this:
/*Main loop*/
bool fFound;
for(int y = 0; y < 100 && !fFound; y+=10)
{
/*Sub loop*/
for (int x = 0; x < 100; x += 10)
{
if(x == 56)
{
//Break the main loop
fFound = true;
break; //Break inner loop
}
}
}
Upvotes: 0
Reputation: 17411
Not advisable but you can use goto
. See this.
public class GotoTest1
{
static void Main()
{
int x = 200, y = 4;
int count = 0;
string[,] array = new string[x, y];
// Initialize the array:
for (int i = 0; i < x; i++)
for (int j = 0; j < y; j++)
array[i, j] = (++count).ToString();
// Read input:
Console.Write("Enter the number to search for: ");
// Input a string:
string myNumber = Console.ReadLine();
// Search:
for (int i = 0; i < x; i++)
{
for (int j = 0; j < y; j++)
{
if (array[i, j].Equals(myNumber))
{
goto Found;
}
}
}
Console.WriteLine("The number {0} was not found.", myNumber);
goto Finish;
Found:
Console.WriteLine("The number {0} is found.", myNumber);
Finish:
Console.WriteLine("End of search.");
// Keep the console open in debug mode.
Console.WriteLine("Press any key to exit.");
Console.ReadKey();
}
}
/*
Sample Input: 44
Sample Output
Enter the number to search for: 44
The number 44 is found.
End of search.
*/
Not advised because it makes the flow harder to understand. Other option of course is to set some flag in the inner loop and check it in the outer loop, I discounted that as it's obvious and assume you know it anyway.. :)
Upvotes: 0
Reputation: 12613
Flags, as suggested in the comments, are probably the best method:
boolean someFlag = true;
for(int y = 0; i < 100 && someFlag; y += 10) {
for(int x = 0; x < 100 && somFlag; x += 10) {
if(x == 56)
someFlag = false;
}
}
Upvotes: 2
Reputation: 182753
There's no good generic answer. The 'right way' depends on the real problem. The best way might be to put the outer loop in a function and then use return;
to break out of it. It might be x=100; y=100;
. It might be done=true;
. Heck, it might even be goto
(kidding).
Upvotes: 0
Reputation: 46183
I don't know if there's a way to break out of nested loops in C#, but allow me to suggest a workaround.
You could throw the main loop into a function and return out of that function. You can return false;
to indicate a premature break and return true;
to indicate that the loop went all the way through, if that matters.
Upvotes: 2
Reputation: 70513
/*Main loop*/
for(int y = 0; y < 100; y+=10)
{
bool makeMeBreak = false;
/*Sub loop*/
for (int x = 0; x < 100; x += 10)
{
if(x == 56)
{
//Break the main loop
makeMeBreak = true;
break;
}
}
if (makeMeBreak) break;
}
Upvotes: 0
Reputation: 8255
Some people would shoot me for suggesting the use of the goto
statement, but breaking out of multiple loops is one of the places it can be very useful (and efficient):
/*Main loop*/
for(int y = 0; y < 100; y+=10)
{
/*Sub loop*/
for (int x = 0; x < 100; x += 10)
{
if(x == 56)
{
goto MainLoopDone;
}
}
}
MainLoopDone:
// carry on here
Upvotes: 6
Reputation: 44288
often its better to put this into a separate function and then do a 'return'
void loop_de_loop()
{
for(int y = 0; y < 100; y+=10)
{
/*Sub loop*/
for (int x = 0; x < 100; x += 10)
{
if(x == 56)
{
return;
}
}
}
}
Upvotes: 3
Reputation: 62439
Use a flag to signal termination:
for(int y = 0; y < 100; y+=10)
{
bool flag = false;
for(int x = 0; x < 100; x += 10)
{
if (x == 56)
{
flag = true;
break;
}
}
if(flag) break;
}
Upvotes: 6