Can I use break to exit multiple nested ‘for’ loops?
No, don’t spoil it with a break. This is the last remaining stronghold for the use of goto.
No, don’t spoil it with a break. This is the last remaining stronghold for the use of goto.
Yep, it’s still O(n^2), it has a smaller constant factor, but that doesn’t affect O notation.
Well, goto, but that is ugly, and not always possible. You can also place the loops into a method (or an anon-method) and use return to exit back to the main code. // goto for (int i = 0; i < 100; i++) { for (int j = 0; j < 100; j++) { goto … Read more
for x in xrange(10): for y in xrange(10): print x*y if x*y > 50: break else: continue # only executed if the inner loop did NOT break break # only executed if the inner loop DID break The same works for deeper loops: for x in xrange(10): for y in xrange(10): for z in xrange(10): … Read more
I agree with the answer currently proposed by fedorqui in the context of the question currently asked. The below is given only to provide some more general answers. One more general approach (for bash 4.0 or newer) is to store your pairs in an associative array: declare -A pairs=( [4_1]=4_2 [5_1]=5_2 [6_1]=6_2 [7_1]=7_2 [8_1]=8_2 ) … Read more
How do I break out of nested loops in Java?
How to break out of multiple loops?
This would be the equivalent in python. In range(0, height, cellSize), 0 and height are the bounds of the range, and cellSize is how many out counter increments. for y in range(0, height, cellSize): for x in range(0, width, cellSize): rect(x, 0, cellSize, cellSize)