Logo

Programming-Idioms

  • Lisp
  • C++
  • PHP
  • Java
  • Python
  • Js

Idiom #43 Break outer loop

Look for a negative value v in 2D integer matrix m. Print it and stop searching.

Control flow jumping forward after the end of the outermost loop
OUTER:
for (var i in m) {
   for (var j in m[i]) {
      if (m[i][j] < 0) {
         console.log("Negative value found: "+m[i][j]);
         break OUTER;
      }
   }
}
(loop named outer
      for i below (array-dimension m 0)
      do (loop for j below (array-dimension m 1)
               for v = (aref m i j)
               when (minusp v)
               do (progn
                    (print v)
                    (return-from outer))))
#include <iostream>
auto indices = findNegativeValue (m, 10, 20);
std::cout << m[indices.first][indices.second] << '\n';

std::pair<int, int> findNegativeValue (int **m, int rows, int columns) {
  for (int i = 0; i < rows; ++i) {
    for (int j = 0; j < columns; ++j) {
      if (m[i][j] < 0) return make_pair (i, j);
    }
  }
  throw "No negative value!";
}

Whenever the code is as complicated as you need to break the outer loop, it is the correct time to add a new function.

edit: The above comment is an opinion and adds nothing to presenting a solution to the stated problem. It is a narrow-minded opinion to think such a statement can apply to every conceivable situation.
for ($y = 0; $y < count($m); $y++) {
    for ($x = 0; $x < count($m[$y]); $x++) {
        if ($m[$y][$x] == $v) {
            break 2;
        }
    }
}
mainloop: for(int i=0;i<m.length;i++)
	for(int j=0;j<m[i].length;j++)
		if(m[i][j]<0){
			System.out.println(m[i][j]);
			break mainloop;
		}

mainloop is a label used to refer to the outer loop.
import static java.lang.System.out;
int i, j, M = m.length, N = m[0].length, x;
boolean b = false;
for (i = 0; i < M; ++i) {
    for (j = 0; j < N; ++j)
        if ((x = m[i][j]) < 0) {
            out.println(x);
            b = true;
            break;
        }
    if (b) break;
}
import static java.lang.System.out;
   int i, j, M = m.length, N = m[0].length, x;
a: for (i = 0; i < M; ++i)
       for (j = 0; j < N; ++j)
           if ((x = m[i][j]) < 0) {
               out.println(x);
               break a;
           }

"... The break statement terminates the labeled statement ... Control flow is transferred to the statement immediately following the labeled (terminated) statement."
class BreakOuterLoop (Exception): pass

try:
    position = None
    for row in m:
        for column in m[row]:
            if m[row][column] == v:
                position = (row, column)
                raise BreakOuterLoop
except BreakOuterLoop:
    pass

This is ugly because the pythonic way to solve this problem would be to refactor so that one doesn't have to break out of multiple loops. See PEP3136
from itertools import chain
matrix = [[1,2,3],[4,-5,6],[7,8,9]]
try:
    print(next(i for i in chain.from_iterable(matrix) if i < 0))
except StopIteration:
    pass

We make a generator that will return negative values from a list (and use chain.from_iterable(matrix) to lazily extract the values) and only take one value from it with the next function. The generator will not continue until we call next again.

This will raise StopIteration if it doesn't find the value so we need to account for it.
def loop_breaking(m, v): 
    for i, row in enumerate(m): 
        for j, value in enumerate(row): 
            if value == v: 
                return (i, j)
    return None

print(loop_breaking(([1,2,3],[4,5,6],[7,8,9]), 6))

Rather than set break flags, it is better to refactor into a function, then use return to break from all nested loops.
z = False
for a in m:
    for b in a:
        if z := b < 0:
            print(b)
            break
    if z: break
Outer_loop:
for A in M'Range (1) loop
   Inner_Loop:
   for B in M'Range (2) loop
      if M (A, B) < 0 then
         Put_Line (M (A, B)'Image);
         exit Outer_Loop;
      end if;
   end loop Inner_Loop;
end loop Outer_Loop;

New implementation...
< >
programming-idioms.org