Sujet : Re: Loops (was Re: do { quit; } else { })
De : Keith.S.Thompson+u (at) *nospam* gmail.com (Keith Thompson)
Groupes : comp.lang.cDate : 21. Apr 2025, 23:39:13
Autres entêtes
Organisation : None to speak of
Message-ID : <878qnt16ni.fsf@nosuchdomain.example.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
User-Agent : Gnus/5.13 (Gnus v5.13)
Janis Papanagnou <janis_papanagnou+
ng@hotmail.com> writes:
On 21.04.2025 23:21, Keith Thompson wrote:
[...]
C-style for loops have been used successfully for decades, and have
been adopted by other languages (including bash, which isn't
particularly C-like).
>
I have to disagree on that. First I'm positive that Bash adopted
the Ksh loops (but incompletely!), and not the "C" loops.
Bash has two kinds of for loops. From the bash manual:
for NAME [ [in WORDS ...] ; ] do COMMANDS; done
and
for (( EXPR1 ; EXPR2 ; EXPR3 )) [;] do COMMANDS ; done
I'm referring to the second form, as I'm sure you know. I see
that ksh has something very similar. I have no idea which shell
added the second form earlier, but both are clearly derived from C,
directly or indirectly.
(It looks like B didn't have a "for" loop. The earliest reference
I can find is the 1974 C manual.)
And, as opposed to Ksh (and "C"), Bash doesn't support FP valued
loops.
Right, because Bash doesn't support floating-point at all. And the
three expressions have to be *arithmetic* expressions, so Bash's
C-style for loop isn't as flexible as C's (I hadn't realized that
until I looked it up just now). But you can still do things that
you can't do with a simple counted for loop, for example printing
powers of 3:
for (( i = 1; i < 1000; i *= 3 )) ; do echo $i ; done
(As previously said, Unix shell in general and Bash specifically
is not a good comparison WRT "C" loops.)
My point is that Bash is one of a number of programming languages
that have adopted something very similar to, and obviously inspired
by, C-style for loops, directly or indirectly. I think it's a
reasonably good comparison, but if you don't I'm not going to argue
the point.
-- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.comvoid Void(void) { Void(); } /* The recursive call of the void */