[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

Multiple indices in for loop

mahdert

3/29/2011 6:01:00 PM

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,
4 Answers

Keith Thompson

3/29/2011 6:52:00 PM

0

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.ne...
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Martin Ambuhl

3/29/2011 7:04:00 PM

0

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.

Azazel

3/29/2011 7:27:00 PM

0

On 2011-03-29, mt <mahdert@gmail.com> 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) {}

It -is- possible. In C99, your example may be used as-is. In C90, one
has to declare the variables at the top of the enclosing block:

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

--
Az.

www: http://www.a...
pgp: http://www.a...~azazel/az_key.asc

J. J. Farrell

3/30/2011 4:04:00 AM

0

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"?