[fpc-pascal] Legitimate use of for and break

Steve Litt slitt at troubleshooters.com
Sat Jul 1 22:25:41 CEST 2023

Santi via fpc-pascal said on Sat, 1 Jul 2023 10:22:48 +0200

>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:

>> 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

I agree, although they're less damaging than goto, which I haven't used
since 1982 using 6800 hex machine language, where I learned how much
less damaging jump to subroutine and return from subroutine are than

>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.

I agree, although there are some practical concerns, which you go on to

>But, as any other golden rule, you must know when it makes sense to 
>ignore it.
>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 tend to put continue statements at or near the top of the block, to
summarily rule out some obvious irrelevant iterations without all sorts
of if/then/else nesting. As a practical matter it's more readable and
more maintainable.

Also, when I'm in a rush, I'll do while(True) and then just put one or
more breaks in the block, with the full intention of going back and
doing it the right way, but then forget to. I apologize to the
maintenance programmer who follows me, because this is bad practice.

Sometimes I use breaks when the loop test condition would be so hairy
as to be difficult to understand. This happens a lot when things get
very stateful. But your post got me to thinking that maybe this is a
symptom that I made a poor design.

>I really hate having the read the full code to guess whats happening. 
>Structured programming  is your friend.



Steve Litt 
Autumn 2022 featured book: Thriving in Tough Times

More information about the fpc-pascal mailing list