Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Multiple indices in for loop (http://www.velocityreviews.com/forums/t745959-multiple-indices-in-for-loop.html)

mt 03-29-2011 06:01 PM

Multiple indices in for loop
 
Why is it not possible to use multiple indices in a for loop in C like
in java,

Example:
for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}

Thanks,

Keith Thompson 03-29-2011 06:52 PM

Re: Multiple indices in for loop
 
mt <mahdert@gmail.com> writes:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}


It is, at least in C99. (I pasted your line of code into a C source
file, and gcc compiled it without complaint.)

If you're compiling in C90 mode, or using a compiler that doesn't
support C99, declarations in for loops aren't permitted at all.

But you might want to consider whether this is a good idea. You have a
loop invariant that j == 2 * i, but it's enforced in two different
places: the initial value of j (0), and the third clause of the for loop
(j = 2 * i). If those get out of sync, you've got problems.

For this particular case, you might try:

for (int i = 0; i <= 10; i ++) {
int j = 2 * i;
/* ... */
}

If you're declaring more than one variable in a for loop header,
you're probably trying to be too clever. (Note that you can't
declare, for example, an int and a char* together, just because of
the limitations of the syntax of a declaration).

FYI, the syntax for a for loop (C99 6.8.5p1) is:

for ( expression(opt) ; expression(opt) ; expression(opt) ) statement

for ( declaration expression(opt) ; expression(opt) ) statement

The second form is new in C99; the "declaration" provides the first
semicolon.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Martin Ambuhl 03-29-2011 07:04 PM

Re: Multiple indices in for loop
 
On 3/29/2011 2:01 PM, mt wrote:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i<= 10; i++,j = 2 * i) {}


What makes you think you cannot do this? In pre-C99 compilers you need
to declare i and j outside the loop of course, but any C99 compiler
should like it as it stands.


J. J. Farrell 03-30-2011 04:03 AM

Re: Multiple indices in for loop
 
mt wrote:
> Why is it not possible to use multiple indices in a for loop in C like
> in java,
>
> Example:
> for (int i = 0, j= 0; i <= 10; i++,j = 2 * i) {}
>
> Thanks,


Impossible to answer (unless it does something different in Java than in
C, in which case I don't know).

Is it coincidence that "mt" is next door to "mu"?


All times are GMT. The time now is 10:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.