Forum > General

break/continue nested loop

(1/1)

riwu:
Are there constructs in FPC to break/continue multi/nested loops?
Or are using flags/labels the only way?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---    for i:=0 to 10 do  begin    for j:=0 to 10 do    begin      if someCondition then        break(2); //break out of both loops, go to methodC       if someCondition2 then        continue(2); //break out if j loop and skip methodB       methodA();    end;     methodB();  end;   methodC();

eny:
YAGNI

GetMem:

--- Quote ---@riwu
Are there constructs in FPC to break/continue multi/nested loops?
--- End quote ---
No.


--- Quote ---@riwu
Or are using flags/labels the only way?

--- End quote ---
Never do that! Break down the lops to procedures/functions instead:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function LoopJ: Integer;var  J: integer;begin  Result := -1; //default, no special attention needed  for J := 0 to 10 do  begin    if SomeCondition1 then    begin      Result := 0;      Exit; //break out of both loops, go to methodC    end;     if SomeCondition2 then    begin      Result := 1;      Exit; //break out j loop and skip methodB    end;    MethodA;  end;end; procedure LoopI;var  I, Res: Integer;begin  for I := 0 to 10 do  begin    Res := LoopJ;    case Res of          0: Exit;      1: Continue;    end;    MethodB;    //you can add other stuff here  end;end; procedure TForm1.Button1Click(Sender: TObject);begin  LoopI;  MethodCend;   

guest58172:

--- Quote from: eny on December 26, 2015, 09:23:06 am ---YAGNI

--- End quote ---

Some languages features look superfluous but when you need them you're happy to have them.

eny:

--- Quote from: BBasile on December 26, 2015, 11:40:24 am ---Some languages features look superfluous but when you need them you're happy to have them.
--- End quote ---
We're not talking about 'some superfluous language features' but about a specific (albeit theoritical and contrived and therefore rather superfluous) question from TS.
Getmem already answered more comprehensively the direction TS needs to lean towards.

Navigation

[0] Message Index

Go to full version