On Sun, 19 Mar 2023 17:49:10 +0000
Bram Moolenaar <[email protected]> wrote:
> Ernie Rael wrote:
> > I need to make a change, the most clear (I think) is
> >
> >      for (i = 0; i < len; i++, idx++) {
> >          // stuff
> >
> >          if (cond)
> >              continue;
> >
> >          // more stuff
> >      }
>
> The "for" statement in C is easily abused to all kinds of things it
> wasn't intended for.  The three parts inside the parens are:
>
> - Initialization, usually setting a variable to a start value.
> - Condition for continuing the loop, should use the variable that was
>   initialized.
> - Advancing, usually incrementing the initialized variable.
>
> Adding an increment in the third part that is not for the loop
> variable makes it harder to understand.  You have to look carefully
> to see what happens.  I rather avoid this.
>
> I know some people want to do this anyway, so that "continue" executes
> the statement.  That avoids something complicated inside the loop,
> thus it can be "less worse".  In your example it's simple enough to
> do:
>
>       for (i = 0; i < len; i++)
>       {
>           // stuff
>
>           if (!cond)
>           {
>              // more stuff
>           }
>           ++idx;
>       }
>
> With more conditions it can get complicated and moving "++idx" to the
> for loop statement can be simpler.

Did you change Ernie's

            if (cond)
                continue;
            // more stuff

to your

>           if (!cond)
>           {
>              // more stuff
>           }

for any specific reason?  For me, "continue" is the better approach,
because it reduces a block/scope.  I prefer

    for (i = 0; i < len; i++) {
        // stuff
        idx++;

        if (cond)
            continue;

        // more stuff
    }

I agree with you, Bram, that 'idx' shouldn't be in for block, simply for
readability.  Honestly, as soon as I see "for (i", most of the time I
don't even read the rest of the line; easy to miss the 'idx++'.

--
Enan
[email protected]
https://www.github.com/3N4N

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/vim_dev/20230320173428.740d7b69%40cirdan.

Raspunde prin e-mail lui