Sujet : Re: Loops (was Re: do { quit; } else { })
De : janis_papanagnou+ng (at) *nospam* hotmail.com (Janis Papanagnou)
Groupes : comp.lang.cDate : 25. Apr 2025, 06:46:21
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vuf7jf$3arcr$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0
On 24.04.2025 22:35, Rosario19 wrote:
[ about gotos ]
for loop is easy because propose always the same type of loop, the
initialization, the condition, the increment etc
I wouldn't call it "the same type of loop", given that you can
formulate different types of loops (increments, shifts, other
operations, etc.). I think the "same type of loop _structure_"
might describe it better.
all ok but sometimes
one need to exit the loop before the main condition of exit,
The comparably rare cases for early exits can (in "C") be done
also with 'break' statements.
Other methods are a reconsideration whether a structural change
of the code would be more natural; we often see code evolving
and it's IME not always clear from the beginning whether a
'for' or a 'while' or a 'do-while' (or something else) is best.
If we're _designing_ the code from scratch [before coding] (as
opposed to "evolutionary" writing the code) the "right" control
constructs usually appear even more naturally.
But given that there's various ways of developing code (beyond
just writing it up as one thinks, as opposed to a design with
various top-down methods) that will lead to different results.
and go down or up in the code lines going out of that loop,
I think there was a discussion some time ago about languages
(or a discussion about the "C" language evolution?) that allow
to exit loops, and the various allowed ways; one was (AFAIR)
to allow [for reasons presented there] only forward exits, for
example.
Especially if you have backward 'goto's it's IMO worth to have
a second look on your iteration construct.
for these cases it is better for me, not use forloop but the right
if()s goto labels...
I'll not (re-)open the can of a fundamental discussion on that.
Use what suits you, and what fits in your [professional/private]
coding environment.
Janis