[fpc-pascal] Legitimate use of for and break
svaa at ciberpiula.net
Sat Jul 1 10:22:48 CEST 2023
El 16/06/2023 a las 16:09, Mattias Gaertner via fpc-pascal escribió:
> On Fri, 16 Jun 2023 20:51:42 +0700
> Hairy Pixels via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>> On Jun 16, 2023, at 6:23 PM, Thomas Kurz via fpc-pascal
>>> <fpc-pascal at lists.freepascal.org> wrote:
>>> Whether it's elegant is a different question. In my opinion YES
>>> because it often gives better readable code than nested "if"
>>> statements inside the loop. But I've also read that using "break"
>>> is discouraged because it shows a bad choice of the loop range.
>> This is highly suspect. Doing an early break in loops is the essence
>> of how to do linear searching. No idea who thinks that's a bad idea.
> 20 years ago there were some programmers, claiming a loop condition
> must only be at start or end, but not in the middle.
I mostly agree with that programmers. That's called structured
programming. "Break" and "continue" are in fact, a subset of GOTO
When you see a structure, a block, you know at the beginning (or end of
the block) the exit conditions. So you can skip the block and you know
the conditions after the block. It is very useful when you are skimming
the code or debugging. You don't have the investigate the inner loop to
see if there are hidden GOTOs.
But, as any other golden rule, you must know when it makes sense to
I use the break, but only at the beginning of the loop. (or exit in
And sometimes with deep nested loops, but, when I commit such crime, I
highlight it with neon lights in the comments.
I really hate having the read the full code to guess whats happening.
Structured programming is your friend.
> Gladfully, most programmers came to their senses.
> fpc-pascal maillist - fpc-pascal at lists.freepascal.org
More information about the fpc-pascal