Breaking or continue from nested loop lazarus / 61-Beginners ---------------------------------------------------------------------- Flaze07 · 2018-06-08 14:09 I need to break or continue from nested loop, I found a thread on it before, http://forum.lazarus.freepascal.org/index.php?topic=30867.0 it suggest breaking into function or procedure. Well I am using the code for https://www.codechef.com/problems/SUBINC and so I need it to be fast. ( I used Dlang before pascal, and my code was too slow ), so is using flag or goto better ? or maybe breaking it into function or procedure don't have that much overhead ? ---------------------------------------------------------------------- Handoko · 2018-06-08 14:21 Please show us your code, we may able to help you optimize it. Programmers usually avoid using goto nowadays, because improper use of it can make the code hard to debug and maintain. ---------------------------------------------------------------------- Flaze07 · 2018-06-08 14:30 well...I am not that done with the code. oh well, I am going to show two codes, Dlang and Pascal https://www.codechef.com/viewsolution/18828694 ( this is the Dlang ) https://gist.github.com/Flaze07/2d544f82b2f06c5f91e6410e8b27e4ad ( this is Pascal, I managed to somewhat convert it, but it is still not correct ) ---------------------------------------------------------------------- Flaze07 · 2018-06-08 15:03 I managed to convert it good, but it turned out to be slower than Dlang counterpart https://www.codechef.com/viewsolution/18831781 oh well, time to try it out with good ol' C++, or maybe there's something in the pascal version that is slowing the whole thing down ? ---------------------------------------------------------------------- marcov · 2018-06-08 15:10 This is a multi level exit (or, GOTO) problem. The code exists the inner-for, but also aborts the outer-for body skipping the nascending statement. FPC has no multilevel exit except "exit' that exits a whole procedure. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure doinner;begin  for inner ...      begin          if something then          exit        end;nAscending:=nAscending+1;end; begin  for outer:=....          doinner;end; Or you could try with goto. ---------------------------------------------------------------------- Flaze07 · 2018-06-08 15:14 well, in the end I used flag...not sure if that is good, goto seems like a good approach too though, I don't know if I want to consider using procedure as using just one function that I made seems to make the Pascal program slower than Dlang's version of the same program ---------------------------------------------------------------------- marcov · 2018-06-08 15:22 Don't forget to flag it as inline, and configure the compiler (lazarus menus or cmdline) to inline As why it is slower, if you translate foreign concepts into a different language, there is a chance that it is not the best option for that language. It might also be unrelated parts (like stdio being slower by default, or memory management being more conservative, thus growing arrays in small increments is slower). Benchmarking is an art. Once we had a fairly major issue with a large scale FPC user saying the code was slower than Kylix/Delphi. Turned out to be that differences in the speed of random() used in the tests that created the main difference, not the algorithm/codegeneration. (FPC has a higher quality but slower random) ---------------------------------------------------------------------- Flaze07 · 2018-06-08 15:27 yeah, I suppose you are right, might try what you suggested. Also, I am not that good with figuring out new ways of doing things, but then again, I don't really expect the pascal code to be faster than the Dlang code, I expect to be about as fast ---------------------------------------------------------------------- Thaddy · 2018-06-08 15:29 The procedural approach is achievable when using inlined functions that return a boolean instead of a procedure. Depending on the boolean return value you can do further breaks/continue on higher levels. Another (or combined) approach is to use local procedures or functions (with e.g. an assignable typed constant in the main block). You can achieve the same effect. But technically that is also flag. ---------------------------------------------------------------------- Flaze07 · 2018-06-08 15:37 ok, thank you for all the help, it's been helpful ---------------------------------------------------------------------- ASerge · 2018-06-09 12:17 Quote from: Flaze07 on June 08, 2018, 04:30:40 pmwell...I am not that done with the code. oh well, I am going to show two codes, Dlang and Pascal https://www.codechef.com/viewsolution/18828694 ( this is the Dlang ) https://gist.github.com/Flaze07/2d544f82b2f06c5f91e6410e8b27e4ad ( this is Pascal, I managed to somewhat convert it, but it is still not correct ) 1. Code in Dlang  contains error: Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i secondOuter : for( int j = i + 1; j <= arrLength; ++j ); // must be < allLength2. There is no need to copy each time to a separate array for verification. It is better to do it on it, just pointing out from and to. 3. In the main question I agree with Thaddy - it is better to use a separate function, you can mark it as inline. 4. And most importantly: your solves the problem WRONG! Index j should start from i, not from i +1. Work sample: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;{$APPTYPE CONSOLE}{$MODE OBJFPC} type  TArray = array of Cardinal; function IsAscending(const A: TArray; AFrom, ATo: Integer): Boolean;var  i: Integer;begin  for i := AFrom + 1 to ATo do    if A[i - 1] > A[i] then      Exit(False);  Result := True;end; function ReadArray(out A: TArray): Boolean;var  ArrLength, i: Integer;begin  Readln(ArrLength);  if ArrLength <= 0 then    Exit(False);  SetLength(A, ArrLength);  for i := Low(A) to High(A) do    Read(A[i]);  Readln; // Skip end of line  Result := True;end; procedure DoTest;var  i, j: Integer;  Arr: TArray;  CountOfAscendings: Integer;begin  if not ReadArray(Arr) then  begin  Writeln('Bad array length');    Exit;  end;  CountOfAscendings := 0;  for i := Low(Arr) to High(Arr) do  begin    for j := i to High(Arr) do      if IsAscending(Arr, i, j) then        Inc(CountOfAscendings);  end;  Writeln(CountOfAscendings);end; var  TestsCount: Cardinal;begin  Readln(TestsCount);  while TestsCount > 0 do  begin    DoTest;    Dec(TestsCount);  end;  Readln;end. ---------------------------------------------------------------------- Thaddy · 2018-06-09 12:24 @ASerge It is always nice to see compilable examples.... compliments.