[fpc-pascal] Legitimate use of for and break
giuliano.colla at fastwebnet.it
Tue Jun 20 12:11:52 CEST 2023
Il 20/06/2023 09:05, Hairy Pixels via fpc-pascal ha scritto:
> Educators continuously have stupid ideas that don't work out as intended in the real world. I would love to see them make a real program that does something difficult and not use early breaks.
> I assume them they forbid early exits in functions also or is the loop special for them?
You don't need something difficult to show that avoiding break is stupid!
Just take a loop searching a linked list for a specific item. You must
test two conditions: item found or end of list. But the item you're
searching might be the last one, so you can't test next=Nil at the
beginning of a do-while loop. A repeat-until can't be used because the
list might be empty.
I prefer in those cases use a function, and exit instead of break, so
that the caller can easily test how the loop was terminated, but it's
just a matter of tastes.
Do not do to others as you would have them do to you.They might have different tastes.
More information about the fpc-pascal