Jump to content
Mike Torrettinni

Wow, first time using repeat ... until

Recommended Posts

I just wanted to share that after 15+years of Delphi, today I used repeat until for the first time! 🙂

I scanned my code and in all 100Ks of lines of code not once I used it on my own (I did find it in some methods that I'm not original writer). Actually feels good, as I never found the real use for it, always just For or While.

 

And the reason I used it is because I had a simple loop for which I wasn't sure when it will end, so I didn't think about all conditions - which would be the case to use While loop and pre-setting all conditions at the beginning.

So, here is my first real use case for repeat until:

// Add Parents in reversed order
repeat
  Result := [aNode.ParentNode] + Result;
  aNode  := aNode.Parent;
until (aNode.ParentNode = nil) or (aNode.ParentNode.NodeType = ntDocument) or (aNode.ParentNode.NodeLevel = 0);

Here I was allowed to add conditions at the end, since I didn't know all of them at the beginning. And it became this beauty of 4 lines 🙂

 

Happy day! 🙂

  • Haha 1

Share this post


Link to post
17 minutes ago, Mike Torrettinni said:

first time

 

Used my first 'goto' a couple of weeks ago 🙂

  • Like 1
  • Haha 1

Share this post


Link to post

then also try out "while .. do" loops, they can replace for and repeat loops both :classic_biggrin:

Share this post


Link to post
9 hours ago, FredS said:

Used my first 'goto' a couple of weeks ago

I was tempted yesterday to use it until I figured out how to get the same result (also codegen wise with as little jumps as possible) using loops in a kinda readable way. 😉

Share this post


Link to post

If you have never used a goto, you have never written complex code. 😉

 

(I usually prefer Exit over goto, but Exit is also considered evil.)

Edited by dummzeuch

Share this post


Link to post

I rarely ever use repeat either. Because a repeat loop always iterates at least once. The example of the original poster will throw a GPF if the initial node is the root node.

 

 

I do use exit() , but if it is inside a try/finally block, I always write a remark behind it that it will jump to the next "finally" statement.

 

 

Share this post


Link to post
3 hours ago, Stefan Glienke said:

loops in a kinda readable way.

 

I was adding something to a tested function and the only way I could justify not retesting all of it is by using 'goto'..

Share this post


Link to post
1 hour ago, Mike Torrettinni said:

GPF?

General Protection Fault. An exception, and a term not often seen in current discussions.

  • Thanks 1

Share this post


Link to post
26 minutes ago, Bill Meyer said:

General Protection Fault. An exception, and a term not often seen in current discussions.

Thanks, don't remember last time I saw this.

Share this post


Link to post
Guest

Exit is brilliant in fuctions. Otherwise, i tend to agree it's a bit finicky.

I like repeat for some special cases. But every time i did a repeat it was to get rid of some local vars needed in a while.

Repeat IMHO should require a begin-end block IMHO to be consistent with the other program control keywords.

Share this post


Link to post
Guest

I remember when i was able to use labels for goto instead of only line numbers. WOW - that was a LEAP forward!

I think the language was CBasic and the OS was CP/M.

Edited by Guest
Added compiler and OS.

Share this post


Link to post
20 minutes ago, Dany Marmur said:

Exit is brilliant in fuctions. Otherwise, i tend to agree it's a bit finicky.

I like repeat for some special cases. But every time i did a repeat it was to get rid of some local vars needed in a while.

Repeat IMHO should require a begin-end block IMHO to be consistent with the other program control keywords.

Exit, Break, and Continue all seem to me to be structured GOTOs. They interrupt the flow, but in a well ordered way. Well, at least when used properly, it is well ordered.

  • Like 1

Share this post


Link to post
20 minutes ago, Dany Marmur said:

I think the language was CBasic and the OS was CP/M.

If we discuss having used those, we date ourselves. 😉  I still have my TP1 for Z80 manual.

Share this post


Link to post
On 4/5/2019 at 4:07 PM, Mike Torrettinni said:

GPF?

General Protection Fault.

 

He should have four stars, by now.

  • Haha 1

Share this post


Link to post
On 4/5/2019 at 5:14 PM, Bill Meyer said:

General Protection Fault. An exception, and a term not often seen in current discussions.

Probably because GPF is called Access Violation (AV) on Windows.

Edited by Rudy Velthuis

Share this post


Link to post
On 4/5/2019 at 7:15 PM, Bill Meyer said:

Exit, Break, and Continue all seem to me to be structured GOTOs. They interrupt the flow, but in a well ordered way. Well, at least when used properly, it is well ordered.

So are For, While and Repeat. They are structured GOTOs as well, but with a built-in If. The jump is just hidden a little better.

Share this post


Link to post

The other day, I discovered that you can't use Exit() in a finally section.  Needless to say, I had a second go at that code.

  • Like 1

Share this post


Link to post
On 4/5/2019 at 5:14 PM, Bill Meyer said:

General Protection Fault. An exception, and a term not often seen in current discussions.

 

On 4/6/2019 at 9:07 PM, Rudy Velthuis said:

Probably because GPF is called Access Violation (AV) on Windows.

You're right, that's what it's called nowadays (and it is a much clearer description).  Any idea in what OS version Microsoft renamed it? 

Share this post


Link to post
3 hours ago, A.M. Hoornweg said:

 

You're right, that's what it's called nowadays (and it is a much clearer description).  Any idea in what OS version Microsoft renamed it? 

It's still GPF in my mind! I have no idea when they renamed it. IIRC, GPFs were frequently accompanied by BSODs, which still happen every now and then. (I'll let someone ask what they are... )

Share this post


Link to post
45 minutes ago, David Schwartz said:

It's still GPF in my mind! I have no idea when they renamed it. IIRC, GPFs were frequently accompanied by BSODs, which still happen every now and then. (I'll let someone ask what they are... )

It is still GPF under the covers,  as in the Wikipedia article here:
https://en.wikipedia.org/wiki/General_protection_fault
The jargon comes from the x86 CPU family. The AV term is discussed here:
https://en.wikipedia.org/wiki/Segmentation_fault

Share this post


Link to post

Back to the original topic regarding repeat ... until:

 

I use them very rarely myself.

 

I really miss the for statement from C/C++ in Delphi, because it lets you specify the initial condition as well as the test and incrementor all in the same line.

 

The problem with most loops is you need to do something first to "prime the pump" so to speak, before you can get the while or repeat loop running.  A classic one is iterating through the results of a DB query:

 

qry.Open;
. . .
qry.First();
while not qry.EOF do
begin
  . . .
  qry.Next();
end;

In C/C++ that could be done as follows:

 

for (qry.Open; not qry.EOF; qry.Next) {
  . . .
}

I think this is much nicer.

Edited by David Schwartz
  • Like 1

Share this post


Link to post
1 hour ago, David Schwartz said:

The problem with most loops is you need to do something first to "prime the pump" so to speak, before you can get the while or repeat loop running.

Yes... but this time it was a small function so all checks for valid values were already done and just quick loop was needed. I don't even know why I chose Repeat, but it worked out really well. I just hope I will not get too excited and try to force it instead of While 🙂

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×