Sujet : Re: Loops (was Re: do { quit; } else { })
De : bc (at) *nospam* freeuk.com (bart)
Groupes : comp.lang.cDate : 18. Apr 2025, 18:27:44
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vtu22g$3f353$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla Thunderbird
On 18/04/2025 17:58, Scott Lurndal wrote:
bart <bc@freeuk.com> writes:
On 18/04/2025 14:37, Janis Papanagnou wrote:
>
* Scott Lurndal (SL) makes a claim that all 'real' for-loops have 3
parts, like C's for(A; B; C) [point 1]
Again you mischaracterize what was stated. I simply
pointed out that most major programming languages have three component
looping constructs using the 'for' keyword, including C.
Apart from C, simple iteration via a loop index has 4 things that need to be denoted:
- Some keyword to say this is such a loop
- The loop index variable
- The start value
- The end value
Many can have an /optional/ step (or means to reverse the order) in the odd cases where that is necessary, making 4/5 if you want to be formal.
C is quite different; it is merely 3 arbitrary expressions, the middle one of which is used as a loop terminating condition (tested notionally at the start of the loop).
But in cases where it is used to emulate the kind of loop described above, then the same applies, except that:
- It will always be 5 parts (the step always has to be accounted for)
- Nearly everthing about how it works has to be imparted to the
compiler:
- The actual expression to initialise the variable
- The exact expression used for the terminating condition
- The exact expression used for incrementing to loop index
- The end value that appears in the terminating condition is
frequently offset (also can also be the case in real for-loops
used in 0-based languages)
- A loop which counts down may well have a quite different pattern
from an upward counting one
So a comparison between C for-loops and more formal ones is not that meaningful.