Velocity Reviews > comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)

# comma in a c statement, ie for(i=0,i2=0;i<10;i++,i2)

Test
Guest
Posts: n/a

 12-15-2012
I have:

for (i=0,i2=0;i<10;i++,i2++)
{
.. some code
}

Is "," (comma) oke here?

I am converting below code to for -loop:

i=0;
i2=0;
while (i<10)
{
... some code
i++;
i2++;
}

Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?

Any caveats? I could not find explanation to comma usage (maybe not not looking
hard enough).

Ben Bacarisse
Guest
Posts: n/a

 12-15-2012
Test <test@.nil.invalid.com> writes:

> I have:
>
> for (i=0,i2=0;i<10;i++,i2++)
> {
> .. some code
> }
>
> Is "," (comma) oke here?

Yes, though it's not obvious why you need both i and i2.. A few spaces
might be a good idea, though:

for (i = 0, i2 = 0; i < 10; i++, i2++)

> I am converting below code to for -loop:
>
> i=0;
> i2=0;
> while (i<10)
> {
> ... some code
> i++;
> i2++;
> }

How odd. What's the point of have both i and i2? If either gets
altered in the "some code" part then there is an argument for not
re-writing it as a "for" loop. Generally, readers will expect the
variables mentioned in a "for" loops to be left alone in the body. This
is not rule of the language but it's a good rule of thumb.

Note, also, that your "for" loop will not be the same as the "while"
loop if the while loop uses "continue".

> Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?

i = 0.

> Any caveats? I could not find explanation to comma usage (maybe not
> not looking hard enough).

It is often considered a little confusing since the comma token has lots
of uses in C.

--
Ben.

Malcolm McLean
Guest
Posts: n/a

 12-15-2012
On Saturday, December 15, 2012 1:47:21 PM UTC, Test wrote:
> I have:
>
> for (i=0,i2=0;i<10;i++,i2++)
>
> {
>
> .. some code
>
> }
>
>
> Is "," (comma) oke here?
>

Yes
>
> Any caveats? I could not find explanation to comma usage
>

Comma operators are very seldom used because there's not much need for them.
An exception is in for loops, where you might want to have two loop counters
going at once. Even here, most C programmers prefer the style

for(i=0;i<N;i++)
{
units[i] = 0;
tens[i*10] = 0;
}
--
Visit Malcolm's homepage
http://www.malcolmmclean.site11.com/www

James Kuyper
Guest
Posts: n/a

 12-15-2012
On 12/15/2012 08:47 AM, Test wrote:
> I have:
>
> for (i=0,i2=0;i<10;i++,i2++)
> {
> .. some code
> }
>
> Is "," (comma) oke here?

Yes.

> I am converting below code to for -loop:
>
> i=0;
> i2=0;
> while (i<10)
> {
> ... some code
> i++;
> i2++;
> }
>
> Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?

The expression i=0 is executed first, though in this particular case it
doesn't matter.

> Any caveats? I could not find explanation to comma usage (maybe not not looking
> hard enough).

The expression E1,E2, where E1 is any expression, and E2 is any
expression other than another comma expression, causes expression E1 to
be evaluated. The result of that evaluation is discarded, so it only
make sense to do this if E1 is an expression with side effects, such as
a=3, b++, or printf("error:%g", d). After all side effects of E1 are
complete, expression E2 is evaluated. The value of expression E2 is also
the value of the comma expression as a whole. Thus "a = (b++,c)" is
equivalent to "b++; a=c;". It only makes sense to use a comma expression
when such a re-write is not possible, as in a for() statement such as

A key point to understand is that commas are NOT always part of a comma
expression. The parameters of a function declaration and the arguments
of a function call are separated by commas, and the same is true for
function-like macros. Commas separate the initializers for the elements
of an array or members of a structure. A single declaration can declare
multiple declarators, separated by commas, as in "int a,b;". Commas are
used to separate the members of a union or enumeration. Commas are also
used in _Generic() expressions and _Static_assert() declarations, which
are new features of C2011, syntactically similar to function calls.

If you wish to use a comma expression in one of those contexts, you'll
have to parenthesize it, to prevent misinterpretation of the comma.
printf("hello %s", "world!\n") does something different from
printf( ("hello %s", "world!\n") ).
--
James Kuyper

James Kuyper
Guest
Posts: n/a

 12-15-2012
On 12/15/2012 08:47 AM, Test wrote:
> I have:
>
> for (i=0,i2=0;i<10;i++,i2++)
> {
> .. some code
> }
>
> Is "," (comma) oke here?

Yes.

> I am converting below code to for -loop:
>
> i=0;
> i2=0;
> while (i<10)
> {
> ... some code
> i++;
> i2++;
> }
>
> Which is executed first in for (i=0,i2=0;i<10;i++,i2++)? i2=0 or i=0?

The expression i=0 is executed first, though in this particular case it
doesn't matter.

> Any caveats? I could not find explanation to comma usage (maybe not not looking
> hard enough).

The expression E1,E2, where E1 is any expression, and E2 is any
expression other than another comma expression, causes expression E1 to
be evaluated. The result of that evaluation is discarded, so it only
make sense to do this if E1 is an expression with side effects, such as
a=3, b++, or printf("error:%g", d). After all side effects of E1 are
complete, expression E2 is evaluated. The value of expression E2 is also
the value of the comma expression as a whole. Thus "a = (b++,c)" is
equivalent to "b++; a=c;". It only makes sense to use a comma expression
when such a re-write is not possible, as in a for() statement such as

A key point to understand is that commas are NOT always part of a comma
expression. The parameters of a function declaration and the arguments
of a function call are separated by commas, and the same is true for
function-like macros. Commas separate the initializers for the elements
of an array or members of a structure. A single declaration can declare
multiple declarators, separated by commas, as in "int a,b;". Commas are
used to separate the members of a union or enumeration. Commas are also
used in _Generic() expressions and _Static_assert() declarations, which
are new features of C2011, syntactically similar to function calls.

If you wish to use a comma expression in one of those contexts, you'll
have to parenthesize it, to prevent misinterpretation of the comma.
printf("hello %s", "world!\n") does something different from
printf( ("hello %s", "world!\n") ).
--
James Kuyper

Les Cargill
Guest
Posts: n/a

 12-15-2012
Malcolm McLean wrote:
> On Saturday, December 15, 2012 1:47:21 PM UTC, Test wrote:
>> I have:
>>
>> for (i=0,i2=0;i<10;i++,i2++)
>>
>> {
>>
>> .. some code
>>
>> }
>>
>>
>> Is "," (comma) oke here?
>>

> Yes
>>
>> Any caveats? I could not find explanation to comma usage
>>

> Comma operators are very seldom used because there's not much need for them.
> An exception is in for loops, where you might want to have two loop counters
> going at once. Even here, most C programmers prefer the style
>
> for(i=0;i<N;i++)
> {
> units[i] = 0;
> tens[i*10] = 0;
> }
> --
> Visit Malcolm's homepage
> http://www.malcolmmclean.site11.com/www
>

I also like:
for(i=0;i<N;i++)
{
const int i2 = (i*10);
units[i] = 0;
tens[i2] = 0;
}

which seems silly for that simple of a loop, but
has come in handy ( especially when i2 is assigned
from a function return )

--
Les Cargill

Keith Thompson
Guest
Posts: n/a

 12-15-2012
James Kuyper <(E-Mail Removed)> writes:
[...]
> A key point to understand is that commas are NOT always part of a comma
> expression. The parameters of a function declaration and the arguments
> of a function call are separated by commas, and the same is true for
> function-like macros. Commas separate the initializers for the elements
> of an array or members of a structure. A single declaration can declare
> multiple declarators, separated by commas, as in "int a,b;". Commas are
> used to separate the members of a union or enumeration.

How do commas separate the members of a union?

> Commas are also
> used in _Generic() expressions and _Static_assert() declarations, which
> are new features of C2011, syntactically similar to function calls.

[...]

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

glen herrmannsfeldt
Guest
Posts: n/a

 12-15-2012
Ben Bacarisse <(E-Mail Removed)> wrote:

(snip)

> How odd. What's the point of have both i and i2? If either gets
> altered in the "some code" part then there is an argument for not
> re-writing it as a "for" loop. Generally, readers will expect the
> variables mentioned in a "for" loops to be left alone in the body. This
> is not rule of the language but it's a good rule of thumb.

I have used "for" loops where I needed "continue" to restart
the loop without incrementing the index.

Otherwise, I agree.

-- glen

Ben Bacarisse
Guest
Posts: n/a

 12-15-2012
glen herrmannsfeldt <(E-Mail Removed)> writes:

> Ben Bacarisse <(E-Mail Removed)> wrote:
>
> (snip)
>
>> How odd. What's the point of have both i and i2? If either gets
>> altered in the "some code" part then there is an argument for not
>> re-writing it as a "for" loop. Generally, readers will expect the
>> variables mentioned in a "for" loops to be left alone in the body. This
>> is not rule of the language but it's a good rule of thumb.

>
> I have used "for" loops where I needed "continue" to restart
> the loop without incrementing the index.
>
> Otherwise, I agree.

So when you want that behaviour you'd write:

for (<initialise>; <condition> {
<code that uses continue>
<increment>
}

rather than

<initialise>
while (<condition>) {
<code that uses continue>
<increment>
}

? I don't think I would (and I could make hand-waving arguments about
why I wouldn't) but it's really just a matter to taste.

--
Ben.

Les Cargill
Guest
Posts: n/a

 12-15-2012
Ben Bacarisse wrote:
> glen herrmannsfeldt <(E-Mail Removed)> writes:
>
>> Ben Bacarisse <(E-Mail Removed)> wrote:
>>
>> (snip)
>>
>>> How odd. What's the point of have both i and i2? If either gets
>>> altered in the "some code" part then there is an argument for not
>>> re-writing it as a "for" loop. Generally, readers will expect the
>>> variables mentioned in a "for" loops to be left alone in the body. This
>>> is not rule of the language but it's a good rule of thumb.

>>
>> I have used "for" loops where I needed "continue" to restart
>> the loop without incrementing the index.
>>
>> Otherwise, I agree.

>
> So when you want that behaviour you'd write:
>
> for (<initialise>; <condition> {
> <code that uses continue>
> <increment>
> }
>
> rather than
>
> <initialise>
> while (<condition>) {
> <code that uses continue>
> <increment>
> }
>
> ? I don't think I would (and I could make hand-waving arguments about
> why I wouldn't) but it's really just a matter to taste.
>

As soon as you need a break or continue in the for loop, my
heuristic is to go with a while. But I'm not always consistent

--
Les Cargill