:: Re: [DNG] Icerbergs aren't small ju…
Startseite
Nachricht löschen
Nachricht beantworten
Autor: Rainer Weikusat
Datum:  
To: dng
Betreff: Re: [DNG] Icerbergs aren't small just because they're mostly underwater
Rainer Weikusat <rainerweikusat@???> writes:
> Didier Kryn <kryn@???> writes:
>
> [...]
>
>>> A multi-line version could look like this:
>>>
>>> while (c = *r) {
>>>     ++r;
>>>          if (c == '/') n = r;
>>> }

>>>
>>
>>     It might be done with a for loop.  eg:

>>
>>     for ( ; *r ; ++r) if(*r=='/') n=r;
>> n++;

>
> [...]
>
>> The for loop is the best construct for a loop with an incremental
>> cursor.
>
> That's nicely exemplified by the fact that the code above does a
> redundant increment (or did a redundant increment would it work, the {}
> are missing) solely to work around


[...]

> the "for loop


While making fun of other people's statements in this way may be ... well
... fun, it's not very nice and also not exactly useful.

A C 'for loop' is a pretty strange control construct (one could call it
'overly generic'). It's definition (from K&R 2nd ed) is

    for (expression1; expression2; expression3) statement


    is equivalent to


    expression1;
    while (expression2) {
          statement
          expression3;
    }


    [in absence of a continue in 'statement']


That's a generalization of a loop with the abstract structure

<init stmts>;
while (<test expr>) {
    <body stmts>;
        <step stmts>;
}


with

<init stmts>
    Sequence of statements intializing a set of loop control
    variables.


<test expr>
    Test expression. Used to compare loop control variables or
    values depending on loop control variables with a termination
    condition (or 'continuation condition' for C). If the loop
    should execute once more, the


<body stmts>,
    a sequence of statements making up the loop body, are
    executed. These may perform operation depending on the current
    value of loop control variables but don't modify them
    themselves.


<step stmts>
    Sequence of statements changing the loop control variables
    possibly based on results from the <body stmts> to 'the next
    state' prior to evualting the <test expr> again.


but the C for (;;;) doesn't enforce any of these semantic conventions
but is really more of a macro which tranposes the text inside the (;;;)
as indicated above. Each of the expressions of a for (;;;) may contain
arbitrary C expressions, ie, anything except C control constructs.

If one happens to be writing a loop following the abstract description
given above, for (;;;) can be used to express it fairly
straight-forwardly if it isn't too complicated. The 'classic' example
would be the C-approximation of a counting loop,

for (int i = 0; i < 10; i++) printf("%d\n", i);

But the loop in

static char const *get_name(char const *arg0)
{
    char const *n, *r;


    n = r = arg0;
    while (*r) if (*r++ == '/') n = r;
    return n;
}


is not of this type. It contains an init-statement,

n = r = arg0;

followed by a test expression,

while (*r)

followed by another test of the same value,

if (*r == '/')

followed by a step-statement,

if (*r++ == '/)

followed by an assignment which should only be executed if the test was
true but which should use the value modified by the step expression.