Programming-Idioms

History of Idiom 43 > diff from v14 to v15

Edit summary for version 15 :

Version 14

2015-08-22, 10:33:44

Version 15

2015-08-23, 07:53:17

Idiom #43 Break outer loop

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

Idiom #43 Break outer loop

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

Code
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;
Comments bubble
2 Solutions:
1. do it with one loop.
2. Break both loops with a flag.