Sujet : Re: Loops (was Re: do { quit; } else { })
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.cDate : 16. Apr 2025, 02:41:29
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250415154221.631@kylheku.com>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-04-15, bart <
bc@freeuk.com> wrote:
Note that C's for-loop is dumb; it merely take three expressions A B C
that can be completely unrelated, and arranges them into a loop:
>
A; while (B) {...; C}
- In this proposed arrangement, if the "continue" keyword is used in the
loop, it will not execute C. You need:
A; while (B) {...; contin_001: C}
and then use "goto contin_001". The 001 is for uniqueness within
the function, to which labels are scoped.
- the arrangement doesn't produce macros very well using the
ordinary preprocessor. Suppose we want to create loop which
steps by 1 from hither to yon:
#define loop_start(A, B, C) do { A = B; while (A < C) {
#define loop_end(A) A++; } } while (0)
we end up with two macros, where we have to repeat the variable.
With the for loop we can just do
#define loop(A, B, C) for (A = B; A < C; A++)
Good thing we have that for syntax with its compact, encapsulated
header that holds all the parts needed.
If you keep plugging away with the naked "for" without making yourself a
convenient macro, that's your problem.
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca