meanning of the for loop

Discussion in 'C Programming' started by Rudra Banerjee, Mar 13, 2013.

  1. Hello friends,
    I encountered a C code(not by me, obviously) which uses:
    gchar *key, *val, **kiter;
    gint i;
    char *keys[] = {"id", "type", "author", "year",NULL};
    g_hash_table_iter_init (&iter, table);
    while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    {
    for (kiter = keys, i = 0; *kiter; kiter++, i++)
    {
    ...
    }
    }

    where gchar, gint, g_hash_table are from glib.
    The point is, from my understanding, for loop has the syntax:
    for(i=initial; i=final; increment)

    So, can anyone kindly help me understand what is two initial condition and two increment condition doing here?

    (The code is working fine, with out any error with gcc; I just want to understand it)
    Rudra Banerjee, Mar 13, 2013
    #1
    1. Advertising

  2. Rudra Banerjee

    Lew Pitcher Guest

    On March 13, 2013 12:41, in comp.lang.c, Rudra Banerjee wrote:

    > Hello friends,
    > I encountered a C code(not by me, obviously) which uses:

    [snip]
    > for (kiter = keys, i = 0; *kiter; kiter++, i++)

    [snip]
    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)
    >
    > So, can anyone kindly help me understand what is two initial condition and
    > two increment condition doing here?


    In your example statement
    for (kiter = keys, i = 0; *kiter; kiter++, i++)

    the loop initialization is
    kiter = keys, i = 0;
    which sets the contents of variable kiter to the value of keys, /and/ sets
    the contents of variable i to the value of 0

    The loop test is
    *kiter
    which tests the value of the data pointed to by the pointer kiter, and
    returns true when those contents are non-zero, false when they are zero

    The loop "increment" is
    kiter++, i++
    which increments kiter, and increments i




    --
    Lew Pitcher
    "In Skills, We Trust"
    Lew Pitcher, Mar 13, 2013
    #2
    1. Advertising

  3. Rudra Banerjee

    Mark Bluemel Guest

    On 13/03/2013 16:41, Rudra Banerjee wrote:
    > Hello friends,
    > I encountered a C code(not by me, obviously) which uses:
    > gchar *key, *val, **kiter;
    > gint i;
    > char *keys[] = {"id", "type", "author", "year",NULL};
    > g_hash_table_iter_init (&iter, table);
    > while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    > {
    > for (kiter = keys, i = 0; *kiter; kiter++, i++)
    > {
    > ...
    > }
    > }
    >
    > where gchar, gint, g_hash_table are from glib.
    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)


    Your understanding is weak...

    > So, can anyone kindly help me understand what is two initial
    > condition and two increment condition doing here?


    They aren't conditions, they are expressions.

    The form of the for loop is
    for (initial_expression; test_expression; move_on_expression)
    statement

    where "statement" could be a braced block of statements.

    The expressions are normal C expressions, which allows for the use of
    the comma operator.

    The comma operator allows expressions to be chained - it simply
    evaluates each expression in turn and the value of the last expression
    in the sequence is the value of the overall "comma-ed" expression.

    This is commonly used in for loops as it allows multiple pointers,
    counters etc to be initialised and incremented.
    Mark Bluemel, Mar 13, 2013
    #3
  4. Rudra Banerjee

    Eric Sosman Guest

    On 3/13/2013 12:41 PM, Rudra Banerjee wrote:
    > Hello friends,
    > I encountered a C code(not by me, obviously) which uses:
    > gchar *key, *val, **kiter;
    > gint i;
    > char *keys[] = {"id", "type", "author", "year",NULL};
    > g_hash_table_iter_init (&iter, table);
    > while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    > {
    > for (kiter = keys, i = 0; *kiter; kiter++, i++)
    > {
    > ...
    > }
    > }
    >
    > where gchar, gint, g_hash_table are from glib.
    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)


    Not quite: The `for' statement has the syntax

    for (expr1 ; expr2 ; expr3)
    statement

    .... where each `exprX' can be an arbitrary expression or can
    be omitted. In the code you show, we get

    expr1: kiter = keys, i = 0
    expr2: *kiter
    expr3: kiter++, i++

    The commas you see are not "part of the `for' syntax," but
    "comma operators." An expression using the comma operator
    has the syntax

    operand1 , operand2

    .... and the effect is to evaluate `operand1' and ignore its
    value, then evaluate `operand2' and use its value as the
    result of the whole expression. Returning to your `for'

    expr1: kiter = keys , i = 0
    ^^^^^^^^^^^^ ^^^^^
    operand1 operand2

    .... so the meaning is "Evaluate `keys' and store the value
    in `kiter'; the stored value becomes the value of `operand1'
    and is ignored. Then evaluate `0' and store the value in `i',
    the stored value becomes the value of `operand2' and thus of
    the entire `expr1'." In this case the value of `expr1' is
    not used so it, too, is ignored: `expr1' and the operands
    it contains were evaluated only for their side-effects.

    The meaning of `expr3' is left as an exercise.

    (Actually, `expr1' can be a declaration rather than just
    an expression, but that's a complication for another day.)

    --
    Eric Sosman
    d
    Eric Sosman, Mar 13, 2013
    #4
  5. On Wednesday, March 13, 2013 5:00:59 PM UTC, Mark Bluemel wrote:
    >
    > Your understanding is weak...
    >

    I agree. Thanks for helping
    Rudra Banerjee, Mar 13, 2013
    #5
  6. On Wednesday, March 13, 2013 5:03:51 PM UTC, Eric Sosman wrote:
    Thanks for the detailed explanation.
    Rudra Banerjee, Mar 13, 2013
    #6
  7. Rudra Banerjee <> writes:
    > I encountered a C code(not by me, obviously) which uses:
    > gchar *key, *val, **kiter;
    > gint i;
    > char *keys[] = {"id", "type", "author", "year",NULL};
    > g_hash_table_iter_init (&iter, table);
    > while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    > {
    > for (kiter = keys, i = 0; *kiter; kiter++, i++)
    > {
    > ...
    > }
    > }
    >
    > where gchar, gint, g_hash_table are from glib.
    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)
    >
    > So, can anyone kindly help me understand what is two initial condition
    > and two increment condition doing here?
    >
    > (The code is working fine, with out any error with gcc; I just want to
    > understand it)


    The syntax of a for loop is:

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

    The second form was added in C99; your example doesn't use it.
    Both forms have three clauses, separated by semicolons; the syntax
    for the second form looks a bit funny because the semicolon is part
    of the declaration.

    It's very common for the first clause to be an assignment expression,
    the second to be a comparison of some sort, and the third to
    be an expression with a side effect such as `i++` -- but *any*
    legal expression (or no expression, since all three are optional)
    is permitted in any of the three (except that the second must be
    of scalar type, since it's used as a condition).

    In your case, the first and third clauses happen to be comma
    expressions. The first clauses assigns the value of keys to kiter,
    and 0 to i; the third increments both kiter and i. Both clauses
    are evaluated only for their side effects; any results are discarded.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Working, but not speaking, for JetHead Development, Inc.
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Mar 13, 2013
    #7
  8. Rudra Banerjee

    Shao Miller Guest

    On 3/13/2013 12:41, Rudra Banerjee wrote:
    > Hello friends,
    > I encountered a C code(not by me, obviously) which uses:
    > gchar *key, *val, **kiter;
    > gint i;
    > char *keys[] = {"id", "type", "author", "year",NULL};
    > g_hash_table_iter_init (&iter, table);
    > while (g_hash_table_iter_next (&iter, (void **)&key, (void **)&val))
    > {
    > for (kiter = keys, i = 0; *kiter; kiter++, i++)
    > {
    > ...
    > }
    > }
    >
    > where gchar, gint, g_hash_table are from glib.
    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)
    >


    This is a mistake.

    > So, can anyone kindly help me understand what is two initial condition and two increment condition doing here?
    >
    > (The code is working fine, with out any error with gcc; I just want to understand it)
    >


    for (kiter = keys, i = 0; *kiter; kiter++, i++)
    ^^^^^^^^^^^^^^^^^^^ ^^^^^^ ^^^^^^^^^^^^
    E1 E2 E3

    That E1 and E3 happen to perform assignments is unrelated to the syntax
    of 'for'.

    int keys[42] = { 13, 10, 0 };
    int * kiter;
    int i;

    kiter = keys, i = 0;
    ^^^^^^^^^^^^^^^^^^^
    E1

    This is a valid expression both inside and outside of a 'for' statement,
    as you can see.

    --
    - Shao Miller
    --
    "Thank you for the kind words; those are the kind of words I like to hear.

    Cheerily," -- Richard Harter
    Shao Miller, Mar 13, 2013
    #8
  9. Rudra Banerjee

    BartC Guest

    "Rudra Banerjee" <> wrote in message
    news:...

    > The point is, from my understanding, for loop has the syntax:
    > for(i=initial; i=final; increment)


    You mean i<=final? (Or usually, i<(final+1) since C is zero-based and 0 to
    N-1 loops (ie. i<N) are more common.)

    > So, can anyone kindly help me understand what is two initial condition and
    > two increment condition doing here?


    A C for-loop such as:

    for (a; b; c) d;

    is pretty much equivalent to this sequence:

    a;
    while (b) {
    d;
    c;
    }

    When there is are double terms as in:

    for (a,b; c; d,e) f;

    then it just becomes:

    a,b;
    while (c) {
    f;
    d,e;
    }

    Although in this case the comma-operator is not really needed, and you can
    write a;b; and d;e; instead.

    This tells you how these constructs are evaluated, but as to what they're
    doing, you'll have to ask the author of the code.

    --
    Bartc
    BartC, Mar 14, 2013
    #9
    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. =?Utf-8?B?VGltOjouLg==?=

    Loop the loop...

    =?Utf-8?B?VGltOjouLg==?=, Feb 16, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    1,379
    Karl Seguin
    Feb 16, 2005
  2. Steven

    while loop in a while loop

    Steven, Mar 24, 2005, in forum: Java
    Replies:
    5
    Views:
    2,228
    Tim Slattery
    Mar 30, 2005
  3. -
    Replies:
    12
    Views:
    690
    Remon van Vliet
    Jun 15, 2005
  4. sonet

    The $a have any special meanning ?

    sonet, Jun 30, 2007, in forum: Perl Misc
    Replies:
    4
    Views:
    110
    Peter J. Holzer
    Jul 2, 2007
  5. Isaac Won
    Replies:
    9
    Views:
    371
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page