1

In Java we can make things like:

 String output = "";

 stop: {
 // count 10 lines

 for ( int row = 1; row <= 10; row++ ){
   // count 5 columns

   for ( int column = 1; column <= 5; column++ {
     if ( row == 5 )
       break stop;
     output += "* ";

     }

     output += "\n";
    }
   output += "\nTerminated Ok.";
  }

The result is a print of 10 lines, but it ends at 5:

 * * * * *
 * * * * *
 * * * * *
 * * * * *
 * * * * *

Is there a Delphi equivalent?

NaN
  • 8,596
  • 20
  • 79
  • 153
  • possible duplicate of [Declaring block level variables for branches in delphi](http://stackoverflow.com/questions/6519191/declaring-block-level-variables-for-branches-in-delphi) – AllTooSir Jun 19 '13 at 12:34
  • The code in the question doesn't need a labelled break. Perhaps you should include a more realistic example which does require a labelled break? – David Heffernan Jun 19 '13 at 13:30
  • @TheNewIdiot No, it is not a duplication of that question because it is an entirely different question that has nothing to do with that question you mentioned, about vars declared inside a block in prism. See? – NaN Jun 19 '13 at 16:16
  • 1
    you can also use catched exceptions of custom class for short-cut exit of loops. Wether it is more or less structured, fast and consitutes creative use or abuse of language feature is a matter of dispute. – Arioch 'The Jun 20 '13 at 10:23
  • Yes, that's an exit, but we would have to use `try` on that, right? – NaN Jun 20 '13 at 17:32

1 Answers1

5

Delphi does not support an equivalent to Java's break to label.

Consider the following:

for i := 1 to 10 do
  for j := 1 to 10 do
    if ... then break;

In Delphi the break always breaks out of the innermost statement, the j for loop in the example above.

In Java you can label a block and break out of that labelled block. So in Java you could break out of the i for loop using a labelled break. For example:

outerloop: {
  for (int i = 1; i < 10; i++) {
    for (int j = 1; j < 10; j++) {
      if (...) break outerloop;
  }
}

Such a break is not possible in Delphi. The closest equivalent in Delphi is the dreaded goto, which is still considered harmful.

{$APPTYPE CONSOLE}

uses
  SysUtils, Classes;

var
  i, j: Integer;

label
  outerloopend;

begin
  for i := 1 to 10 do
  begin
    for j := 1 to 10 do
    begin
      if i>5 then
        goto outerloopend;
      Write(Format('%d,%d ', [i, j]));
    end;
    Writeln;
  end;
outerloopend:
  Writeln('Edsger Dijkstra, sorry for using goto!');
  Readln;
end.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • AFAIK the example provided doesn't include a Java label, to me it looks like his just trying to exit the method. – Peter Jun 19 '13 at 13:26
  • @PeterVonča Yes the syntax in the question was wrong. I just fixed it. – David Heffernan Jun 19 '13 at 13:27
  • Are you sure that your edit is correct? In java labels have no begin or end declaration, it would be this instead `Test : break Test` instead of using `{` and `}`. That's why my initial asumption was that he is simply looking for the Exit procedure. – Peter Jun 19 '13 at 13:32
  • @PeterVonča The code in the question is valid, is it not? Nothing wrong with writing single statements inside compound statements and including extraneous braces. – David Heffernan Jun 19 '13 at 13:33