Ernie Rael wrote:
> Is "for (i = 0; i < len; i++, idx++)" OK style in vim?
>
> -ernie
>
> I'm looking at a change to a vim "C" file. It currently looks like
>
> for (i = 0; i < len; i++) {
> // stuff
>
> // more stuff
>
> ++idx;
> }
Except that the "{" should be on the following line.
>
> 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.
--
When danger reared its ugly head,
He bravely turned his tail and fled
Yes, Brave Sir Robin turned about
And gallantly he chickened out
Bravely taking to his feet
He beat a very brave retreat
Bravest of the brave Sir Robin
Petrified of being dead
Soiled his pants then brave Sir Robin
Turned away and fled.
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
/// Bram Moolenaar -- [email protected] -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
--
--
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/20230319174910.AF94C1C135C%40moolenaar.net.