This language bar is your friend. Select your favorite languages!

Idiom #43 Break outer loop

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

Illustration
for ($y = 0; $y < count($m); $y++) {
    for ($x = 0; $x < count($m[$y]); $x++) {
        if ($m[$y][$x] == $v) {
            break 2;
        }
    }
}
#include <stdio.h>
int i,j;
for(i=0;i<sizeof(m)/sizeof(*m);i++)
{
	for(j=0;j<sizeof(*m)/sizeof(**m);j++)
	{
		if(m[i][j]<0)
		{
			printf("%d\n",m[i][j]);
			goto end;
		}
	}
}
end:
#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!";
}
1
import std.writeln;
outer:
foreach (i; 0 .. m.length)
{
	foreach (j; 0 .. m[i].length)
	{
		if (m[i][j] < 0)
		{
			writeln(m[i][j]);
			break outer;
		}
	}
}
OUTER: for (var i = 0; i < m.length; i++) {
  for (var j = 0; j < m[i].length; j++) {
    if (m[i][j] < 0) {
      print("Negative value found at $i,$j: ${m[i][j]}");
      break OUTER;
    }
  }
}
find_negative_in([]) -> undefined;
find_negative_in([Row|Rows]) -> find_negative_in(Row, Rows).

find_negative_in([], Rows) -> find_negative_in(Rows);
find_negative_in([Pos|Values], Rows) when Pos >= 0 ->
	find_negative_in(Values, Rows);
find_negative_in([Neg|_], _) -> Neg.
mainloop:
	for i, line := range m {
		for _, v := range line {
			if v < 0 {
				fmt.Println(v)
				break mainloop
			}
		}
	}
(print . head . filter (<0) . concat) m
OUTER:
for (var i in m) {
   for (var i in m[i]) {
      if (m[i][j] < 0) {
         console.log("Negative value found: "+m[i][j]);
         break OUTER;
      }
   }
}
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;
		}
breakout = false
for i,v1 in ipairs(m) do
   for j,v2 in ipairs(v1) do
      if v2 < 0 then
         print(v2)
         state = "found"
         breakout = true
         break
      end
   end
   if breakout then break end
end
var i,j: integer;
   flag:boolean;
[...]
for i := 0 to length(m) * length(m[0]) - 1 do
begin
  v := m[i mod length(m), i div length(m)];
  if v < 0 then
  begin
    writeln(v);
    break;
  end;
end;

for i := 0 to high(m) do
begin
  for v in m[i] do
  begin
    flag := (v < 0);
    if flag then
    begin
      writeln(v);
      break;
    end;
  end;
  if flag then
    break;
end;
OUTER:
for my $row (@m) {
   for $v (@$row) {
      if ($v < 0) {
         print "Negative value found: $v\n";
         last OUTER;
      }
   }
}
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
negative_value = catch :negative do
  matrix.each do |row|
    row.each do |value|
      throw :negative, value if value < 0
    end
  end
end

puts negative_value
'outer: for v in m {
        'inner: for i in v {
            if i < 0 {
                println!("Found {}", i);
                break 'outer;
            }
        }
    }

Do you know the best way to do this in your language ?
New implementation...

Idiom created by

programming-idioms.org

Related idioms