Forum > Beginners

Breaking or continue from nested loop

(1/3) > >>

Flaze07:
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:
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:
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:
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:
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  [+][-]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";}};} ---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.

Navigation

[0] Message Index

[#] Next page

Go to full version