meanning of the for loop

R

Rudra Banerjee

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)
 
L

Lew Pitcher

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
 
M

Mark Bluemel

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.
 
E

Eric Sosman

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.)
 
R

Rudra Banerjee

On Wednesday, March 13, 2013 5:03:51 PM UTC, Eric Sosman wrote:
Thanks for the detailed explanation.
 
K

Keith Thompson

Rudra Banerjee said:
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.
 
S

Shao Miller

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.
 
B

BartC

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top