dummzeuch 1505 Posted April 9, 2019 (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 April 9, 2019 by Lars Fosdal Assisted with deleting that code section Share this post Link to post
Bill Meyer 337 Posted April 9, 2019 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
David Schwartz 426 Posted April 10, 2019 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. 1 Share this post Link to post
Rudy Velthuis 91 Posted April 16, 2019 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". 1 Share this post Link to post
David Schwartz 426 Posted April 16, 2019 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. 1 Share this post Link to post
Rudy Velthuis 91 Posted April 18, 2019 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
David Schwartz 426 Posted April 18, 2019 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
Rudy Velthuis 91 Posted April 20, 2019 (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 April 20, 2019 by Rudy Velthuis Share this post Link to post
David Heffernan 2345 Posted April 20, 2019 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. 2 Share this post Link to post
Lars Fosdal 1792 Posted April 22, 2019 @David Heffernan, isn't there a variation where the iteration is done before the statement? Share this post Link to post
David Heffernan 2345 Posted April 22, 2019 1 hour ago, Lars Fosdal said: @David Heffernan, isn't there a variation where the iteration is done before the statement? No. The only C++ loop with an interaction statement is for. Share this post Link to post
Lars Fosdal 1792 Posted April 22, 2019 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
Rudy Velthuis 91 Posted April 23, 2019 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
Rudy Velthuis 91 Posted May 1, 2019 On 4/17/2019 at 12:46 AM, David Schwartz said: Why does Pascal have three distinct forms of loops FWIW, Pascal only has two: for-to and for-downto. The for loop in Pascal is quite simple, but in the olden days, I guess the fact that it was easier to write and the fact that is was dedicated to increment or decrement an integer to a preset limit made it faster too. These days, with optimizing compilers, the difference is not that big anymore. Note that the synatx is very similar to what other non-C-style languages had, like Basic or several versions of Algol (which predate Pascal, AFAIK). The third way, for-in, is a Delphi/FreePascal/Some_Other_Dialect construct only. More here: https://en.wikipedia.org/wiki/For_loop Share this post Link to post
David Schwartz 426 Posted May 1, 2019 53 minutes ago, Rudy Velthuis said: FWIW, Pascal only has two: for-to and for-downto. I was referring to for, while...do, and repeat...until. Share this post Link to post
Rudy Velthuis 91 Posted May 1, 2019 (edited) 2 hours ago, David Schwartz said: I was referring to for, while...do, and repeat...until. Oh yes, of course. I misread. Sorry. I don't think I can cancel (delete) that message? Edited May 1, 2019 by Rudy Velthuis Share this post Link to post