# 打破了嵌套的循环

` ` // goto for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { goto Foo; // yeuck! } } Foo: Console.WriteLine("Hi");` `

VS：

` `// anon-method Action work = delegate { for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { return; // exits anon-method } } }; work(); // execute anon-method Console.WriteLine("Hi");` `

` `// local function (declared **inside** another method) void Work() { for (int x = 0; x < 100; x++) { for (int y = 0; y < 100; y++) { return; // exits local function } } }; Work(); // execute local function Console.WriteLine("Hi");` `

` ` for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { if (exit_condition) { // cause the outer loop to break: i = INT_MAX; Console.WriteLine("Hi"); // break the inner loop break; } } }` `

` `outer: while(fn1()) { while(fn2()) { if(fn3()) continue outer; if(fn4()) break outer; } }` `

` `bool exitedInner = false; for (int i = 0; i < N && !exitedInner; ++i) { .... some outer loop stuff for (int j = 0; j < M; ++j) { if (sometest) { exitedInner = true; break; } } if (!exitedInner) { ... more outer loop stuff } }` `

` `for (int i = 0; i < N; ++i) { .... some outer loop stuff if (!doInner(i, N, M)) { break; } ... more outer loop stuff }` `

` `for ( int i = 0; i < 10; ++i ) { for ( int j = 0; j < 10; ++j ) { // code if ( break_condition ) goto End; // more code } } End: ;` `

` `bool exit = false; for ( int i = 0; i < 10 && !exit; ++i ) { for ( int j = 0; j < 10 && !exit; ++j ) { // code if ( break_condition ) { exit = true; break; // or continue } // more code } }` `

` `try { for ( int i = 0; i < 10 && !exit; ++i ) { for ( int j = 0; j < 10 && !exit; ++j ) { // code if ( break_condition ) { throw new Exception() } // more code } } catch ( Exception e ) {}` `

` `def do_until_equal(): foreach a: foreach b: if a==b: return` `

` `foreach (var item in array) { // ... break; // ... }` `

` `for (int i=0; i<array.Length; ++i) { var item = array[i]; // ... break; // ... }` `

` `int i=0; while (i < array.Length) { var item = array[i]; // ... break; // ... ++i; }` `

` ` int i=0; // for initialization startLoop: if (i >= array.Length) // for condition { goto exitLoop; } var item = array[i]; // ... goto exitLoop; // break // ... ++i; // for post-expression goto startLoop;` `

.NET JIT编译器是一个SSA编译器。 我不会在这里详细介绍SSA的forms，以及如何创build一个优化的编译器，这只是太多了，但是可以对将要发生的事情给出基本的了解。 为了更深入的理解，最好开始阅读优化编译器（我喜欢这本书的简要介绍： http ://ssabook.gforge.inria.fr/latest/book.pdf）和LLVM（llvm.org） 。

` ` int i=0; // for initialization if (i >= array.Length) // for condition { goto endOfLoop; } startLoop: var item = array[i]; // ... goto endOfLoop; // break // ... ++i; // for post-expression if (i >= array.Length) // for condition { goto startLoop; } endOfLoop: // ...` `

` `// a is a variable. for (int i=0; i<100; ++i) { for (int j=0; j<100; ++j) { // ... if (i*j > a) { // break everything } } }` `

` `int i, j; for (i=0; i<100 && i*j <= a; ++i) { for (j=0; j<100 && i*j <= a; ++j) { // ... } }` `

` `// Outer loop in method 1: for (i=0; i<100 && processInner(i); ++i) { } private bool processInner(int i) { int j; for (j=0; j<100 && i*j <= a; ++j) { // ... } return i*j<=a; }` `

` `bool more = true; for (int i=0; i<100; ++i) { for (int j=0; j<100; ++j) { // ... if (i*j > a) { more = false; break; } // yuck. // ... } if (!more) { break; } // yuck. // ... } // ...` `

• 编译器会将所有内容写出来作为分支。
• 作为优化步骤，编译器将执行数据stream分析，尝试去除仅在控制stream中使用的奇怪的`more`variables。
• 如果成功，variables将从程序中消除，只剩下分支。 这些分支将被优化，所以你将只从内部循环中得到一个分支。
• 如果没有成功，那么最内层循环肯定会使用variables，所以如果编译器不会优化它，那么分配给寄存器（这会浪费宝贵的寄存器内存）的机会很大。

` `for (int i=0; i<100; ++i) { for (int j=0; j<100; ++j) { // ... if (i*j > a) { goto exitLoop; } // perhaps add a comment // ... } // ... } exitLoop: // ...` `

TL;博士：

• 如果可能的话，在for循环中使用一个简单的条件。 尽可能地坚持你所掌握的高级语言结构。
• 如果一切都失败了，而你离开了`goto`或者`bool more` ，那么更喜欢前者。

` `public void GetIndexOf(Transform transform, out int outX, out int outY) { outX = -1; outY = -1; for (int x = 0; x < Columns.Length; x++) { var column = Columns[x]; for (int y = 0; y < column.Transforms.Length; y++) { if(column.Transforms[y] == transform) { outX = x; outY = y; return; } } } }` `

` `for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { i = 100; break; } }` `

` `while( some_condition ) { // outer loop stuff ... bool get_out = false; for(...) { // inner loop stuff ... get_out = true; break; } if( get_out ) { some_condition=false; continue; } // more out loop stuff ... }` `

` `break; // our trusty friend, breaks out of current looping construct. break 2; // breaks out of the current and it's parent looping construct. break 3; // breaks out of 3 looping constructs. break all; // totally decimates any looping constructs in force.` `

` `var isDone = false; for (var x in collectionX) { for (var y in collectionY) { for (var z in collectionZ) { if (conditionMet) { // some code isDone = true; } if (isDone) break; } if (isDone) break; } if (isDone) break; }` `

…我希望对那些喜欢我的人来说，这是帮助“反风扇”:)

` `try { foreach (object o in list) { foreach (object another in otherList) { // ... some stuff here if (condition) { throw new CustomExcpetion(); } } } } catch (CustomException) { // log }` `

` ` for (; j < 10; j++) { //solution bool breakme = false; for (int k = 1; k < 10; k++) { //place the condition where you want to stop it if () { breakme = true; break; } } if(breakme) break; }` `

` ` bool breakInnerLoop=false for(int i=0;i<=10;i++) { for(int J=0;i<=10;i++) { if(i<=j) { breakInnerLoop=true; break; } } if(breakInnerLoop) { continue } }` `

` `<?php for(...) { while(...) { foreach(...) { break 3; } } }` `