Multiple indices in for loop

Discussion in 'C Programming' started by mt, Mar 29, 2011.

  1. mt

    mt Guest

    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,
    mt, Mar 29, 2011
    #1
    1. Advertising

  2. mt <> 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) <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"
    Keith Thompson, Mar 29, 2011
    #2
    1. Advertising

  3. 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.
    Martin Ambuhl, Mar 29, 2011
    #3
  4. 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"?
    J. J. Farrell, Mar 30, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Justin
    Replies:
    1
    Views:
    462
    Gunnar Hjalmarsson
    Jun 3, 2004
  2. Ross MacGregor
    Replies:
    5
    Views:
    323
    Ross MacGregor
    Aug 25, 2003
  3. SnuSnu
    Replies:
    6
    Views:
    334
    Steven Rumbalski
    Apr 25, 2004
  4. Johannes Ahlmann

    loop over array with indices

    Johannes Ahlmann, May 23, 2005, in forum: Ruby
    Replies:
    0
    Views:
    108
    Johannes Ahlmann
    May 23, 2005
  5. Isaac Won
    Replies:
    9
    Views:
    364
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page