Recent

Author Topic: Does FPC fully support preserving the FOR variable when a GOTO or BREAK is used?  (Read 2988 times)

creaothceann

  • Full Member
  • ***
  • Posts: 198
Ok found the relevant section in Reference guide for Free Pascal, version 3.2.2. It's an old copy, but still useful. I thought it was the elusive Programmers Reference Manual, which sadly I don't have a copy of (have had a few searches online, but no luck).

https://www.freepascal.org/docs-html/current/prog/prog.html
https://www.freepascal.org/docs-html/current/
And don't start an argument, I am right.

old_DOS_err

  • New Member
  • *
  • Posts: 44
Thanks creaothceann for the links, booked marked those. But I was hoping there might be some meaty PDF lurking out in the shadows :) as I often work offline.

Thaddy

  • Hero Member
  • *****
  • Posts: 18306
  • Here stood a man who saw the Elbe and jumped it.
All these are also downloadable as pdf.
He just left out the correct link for the pdf's:
https://www.freepascal.org/docs.html

Anyway you seem to be a master in not finding the official documentation... O:-)
« Last Edit: October 08, 2025, 06:45:18 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12523
  • FPC developer.
And lazarus installers on sane OSes hopefully come with browsable CHM help.

old_DOS_err

  • New Member
  • *
  • Posts: 44
Thanks Thaddy, I downloaded a few of them. I'll go and stand on the naughty step when I finished typing this  :)

But you aren't wrong, not just with pascal. Even though I am English, my online searches often miss the target. It's one of key reasons I went into programming when I left school (a very long time ago), because I struggle so much to communicate with people. Hark, the sound the violins.

Zoran

  • Hero Member
  • *****
  • Posts: 1974
    • http://wiki.lazarus.freepascal.org/User:Zoran
I hope you don't really use the counter variable after the for loop, do you?

Even when you use it correctly, when you know the loop was left with break, it's less readable, whenever you later need to read this part of the code, you again have to make sure it is used correctly. Not to mention that someone else should understand this code, too.

It's a simple rule - if you need to use the counter variable, do not use for, use while. That's just it.

Every for loop can be easily converted to while loop. Just always do it if you need the counter variable after the loop is finished.

Yes, it is documented that you can rely on it if the loop was left with break, but never use it, it's bad practise.
Make it a rule and stick to it, make no exceptions, just always convert for into while.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

PascalDragon

  • Hero Member
  • *****
  • Posts: 6184
  • Compiler Developer
For that value to be undefined at the conclusion of the For loop, if it is allowed to run fully, it implies the value of iteration is being placed into the ECX register (CX in 8086, unsure about RCX, I still live in 16 bits with assembler, let’s just say ECX to keep it simple) and a standard assembler LOOP is being run.

Which makes sense. What it does also explain is why upon completion the variable has the maximum value, not one over. Usually if one is using a Repeat or While and a single step iteration, the variable, if the loop runs fully, is 1 over. Which is another reason the counter variable in a For cannot be used to determine certain actions (like testing if an item is in a list).

You can not make any assumption how the compiler implements the loop. In fact it doesn't use ECX or RCX unless it just happens that the compiler picks that register for the variable.

And the variable being undefined simply means that you can not rely on the value contained in the variable for example, because the compiler might turn an upwards loop into a downwards loop.

old_DOS_err

  • New Member
  • *
  • Posts: 44
Hi PascalDragon,

Yep, sorry. Got carried away with my inner speculating. I can see what you are saying. I'm new to this forum stuff, it feels frustrating to have the coding experience I have, but it being 20 years out of date. It would be nice to put something back, given how much I have gotten out of the forum, not just my own questions, but from searches online. But I have to accept that I don't even know most of the modern terminolgy, let alone how free pascal actually works. I will try to stick to just asking questions.

dbannon

  • Hero Member
  • *****
  • Posts: 3556
    • tomboy-ng, a rewrite of the classic Tomboy
It's a simple rule - if you need to use the counter variable, do not use for, use while. That's just it.

Yep, I work to Zoran's rule. While you may check carefully that the special condition (where the variable is valid) applies, later on, someone else, or even your self, may extend that bit of code to outside that condition. And, worse case, it might work on the machine being developed on, and then become a very difficult bug. Simple rules are good rules.

Davo
Lazarus 3, Linux (and reluctantly Win10/11, OSX Monterey)
My Project - https://github.com/tomboy-notes/tomboy-ng and my github - https://github.com/davidbannon

speter

  • Sr. Member
  • ****
  • Posts: 419
... I'm new to this forum stuff, it feels frustrating to have the coding experience I have, but it being 20 years out of date. It would be nice to put something back, given how much I have gotten out of the forum, not just my own questions, but from searches online. But I have to accept that I don't even know most of the modern terminolgy, let alone how free pascal actually works. I will try to stick to just asking questions.
I tihnk many (most?) of those on the forum are "seniors", many are hobbyists, and all are learning new stuff. :) BUT, don't let that stop you from answering questions and/or asking for clarifications. The Dragon may occasionally breathe a bit of fire, but they are always on-topic and really helpful. :)  :o

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

old_DOS_err

  • New Member
  • *
  • Posts: 44
Thanks speter, I appreciate that.

It would be helpful though if I stopped reinventing the wheel every time I fail to get a tool working  ::)

 

TinyPortal © 2005-2018