打破嵌套循环

当有嵌套循环时,有人能告诉我如何打破主循环吗?
例*:

/*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 } } } 

*这段代码什么都不做,这只是一个例子

我应该把它放在“Break main loop”评论的位置? 在java中有标签,我可以打破(当我将标签设置为名为“MainLoop”的主循环时,我可以写“break MainLoop;”并且它将是有效的),但我能在这做什么?

谢谢你的建议!

goto

我不理解这种持续的模因,认为goto被认为是“有害的”。 如果使用得当,它非常强大,就是这种情况。

  • 重构因此您不需要以这种方式退出嵌套循环。
    通常可以通过将循环放入单独的函数来使用return
  • 使用goto
  • 使用旗帜(丑陋)

使用标志来表示终止:

 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; } 

有些人会因为建议使用goto语句而向我goto ,但是打破多个循环是其中一个非常有用(且高效)的地方:

 /*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 

通常最好把它放到一个单独的函数然后做一个’返回’

 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; } } } } 

我不知道是否有办法打破C#中的嵌套循环,但允许我建议一个解决方法。

您可以将主循环抛入函数并返回该函数。 你可以return false; 表示过早rest并return true; 如果重要的话,表明循环已经完成了。

正如评论中所建议的那样,标志可能是最好的方法:

 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; } } 
 /*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; } 

没有好的通用答案。 “正确的方式”取决于真正的问题。 最好的方法可能是将外循环放在函数中然后使用return; 打破它。 它可能是x=100; y=100; x=100; y=100; 。 它可能是done=true; 。 哎呀,甚至可能是goto (开玩笑)。

不建议,但你可以使用goto 。 看到这个 。

 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. */ 

不建议,因为它使流程更难理解。 其他选项当然是在内循环中设置一些标志并在外循环中检查它,我打了折扣,因为它很明显并且假设你知道它反正.. 🙂

因为,正如你所提到的, break命令上没有标签,你可以这样做:

 /*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 } } } 

正如其他人所说,“正确”的答案取决于你正在解决的问题。 如果可以的话,将其分成小块是首选路线。 这个模型的东西:

 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; } 

在我看来,从一个函数中获得多个return语句,使用额外的标志或者( 颤抖 )使用goto是不同程度的丑陋。 但是,有时其中一个是必要的。

编辑:这是演示使用此方法返回某种有用的对象,如果使用此实例,您将使用“Customer”或“IDataAccess”或“bool”或“object”以外的其他内容作为返回类型。

我首先使用LINQ收集有趣的对象,然后对LINQ查询的结果执行操作。 从而删除嵌套循环并替换为一个循环。