Jump to content
Mike Torrettinni

Wow, first time using repeat ... until

Recommended Posts

Posted (edited)
12 hours ago, David Schwartz said:

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


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

I think this is much nicer.

 

Actually, since qry.next will be executed at the end of the loop, I think the C style for loop syntax is inconsistent.

 

(For whatever reason I cannot delete the first code section of the quoted text on mobile.)

Edited by Lars Fosdal
Assisted with deleting that code section

Share this post


Link to post
7 hours ago, dummzeuch said:

Actually, since qry.next will be executed at the end of the loop, I think the C style for loop syntax is inconsistent.

Many years ago, I knew an instructor form UC Santa Cruz who could speak endlessly on the irregularities of C syntax, which are legion.

Share this post


Link to post
On 4/8/2019 at 11:17 PM, dummzeuch said:

Actually, since qry.next will be executed at the end of the loop, I think the C style for loop syntax is inconsistent.

 

Sorry I'm not sure what's your point.

 

I like the way the for loop is structured in C/C++ because all three relevant state mechanisms are there: the initializer; the condition to continue or stop; and the iterator used at the end of the loop.

 

Perhaps it's inconsistent syntactically, but I find it much more consistent in terms of how I think of loops working, since it completely controls the statement or block following it.

 

I can't tell you how many times I forget to put the iterator at the bottom of the loop block in Delphi. It's pretty damn obvious when it's missing in C/C++ FOR statements, and even the compiler can flag it for you. But in Delphi, you get no help from the compiler, and it's not even obvious something might be missing just by looking at the code.

 

And if you happen to put it in the wrong place in the loop block, it can cause you no end of headaches.
 

The C/C++ approach is much safer and easier to see when it's missing or wrong.

 

There's also the problem that the Delphi approach requires you to reference a loop var within the block that may otherwise not even be needed.

  • Like 1

Share this post


Link to post
On 4/8/2019 at 9:35 PM, David Schwartz said:

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

 


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

I think this is much nicer.

The disadvantage of C/C++ for-loops is that the end condition is always evaluated, unlike Pascal's, where it is only evaluated on entry. OTOH, this can be an advantage too. The syntax (init; end condition; looping) is nice, but they should not have called it "for".

Share this post


Link to post
12 hours ago, Rudy Velthuis said:

The disadvantage of C/C++ for-loops is that the end condition is always evaluated, unlike Pascal's, where it is only evaluated on entry. OTOH, this can be an advantage too. The syntax (init; end condition; looping) is nice, but they should not have called it "for".

I don't really care what it's called. Every language has its quirks. Why does Pascal have three distinct forms of loops? Seems like overkill. And each one has its own limitations.

 

What would you have called it? I'd nominate either "loop" or "repeat" -- but this is ONLY for the C/C++ language.

  • Like 1

Share this post


Link to post
On 4/17/2019 at 12:46 AM, David Schwartz said:

Why does Pascal have three distinct forms of loops? Seems like overkill. And each one has its own limitations.

 

So do loops in other languages. And even a simple language like C has while, do while and for loops.  I don't see that as overkill. You use what best suits you. After all, carpenters often have different kinds of hammers (and different kinds of planers) too. <g>

Share this post


Link to post
17 hours ago, Rudy Velthuis said:

So do loops in other languages. And even a simple language like C has while, do while and for loops.  I don't see that as overkill. You use what best suits you. After all, carpenters often have different kinds of hammers (and different kinds of planers) too. <g>

That's true. I guess I spoke too hastily. That said, I rarely use repeat-until in Delphi, and I wish the for loop was more like C's for loop. That's it. 🙂

Share this post


Link to post
Posted (edited)
On 4/18/2019 at 9:08 PM, David Schwartz said:

That's true. I guess I spoke too hastily. That said, I rarely use repeat-until in Delphi, and I wish the for loop was more like C's for loop. That's it. 🙂

I am pretty glad about the Pascal for loop(s) (i.e. for-to/for-downto and for-in), because they have the advantage that, in the case of for-to/for-downto, the end index is fixed, and that it only needs to check an index, not any general condition.


But a loop like the C for-loop would be nice to have additionally. It should just be called differently, e.g. loop:

loop i := 10; i < 20; Inc(i) do

Unfortunately, Delphi does not have a comma operator, so you probably couldn't do:

loop read := 0, write := 0; read < Length(s); Inc(read) do

Although, with that syntax (commas and semicolons as separators) and some extra clever logic in the compiler, it could perhaps even be called for too.

It should of course also know type inference and local inline declaration of variables, like the current for loops in Delphi.

Edited by Rudy Velthuis

Share this post


Link to post

The C++ for loop, in its general form, is trivially translated into a while loop. So

 

for (init; condition; iteration) statement

 

becomes

 

init;

while (condition) {

  statement;

  iteration;

 

It is well worth knowing this if you are in the business of translating pieces of code. 

 

So in C++ the for loop is syntactic sugar on top of the while loop. 

 

In Delphi also the classic for loop can also be trivially expressed as a while loop. This for loop is simpler than that of C++ but it's still just a while loop (condition tested before body executed) with implicit increment. 

 

They Pascal repeat loop is like the C++ do loop. They key being that the condition is at the end of the body rather than the beginning. 

 

 

  • Like 2

Share this post


Link to post
2 hours ago, David Heffernan said:

No. The only C++ loop with an interaction statement is for. 

You are right. I was confusing it with the i++ vs ++i  effects.

Share this post


Link to post
On 4/22/2019 at 9:19 PM, Lars Fosdal said:

You are right. I was confusing it with the i++ vs ++i  effects.

These differences have absolutely no effect in a C++ loop. The increment is considered to be the last line in the (while) loop, and on a line alone, nor i++ nor ++i make a difference.

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

×