Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   meanning of the for loop (http://www.velocityreviews.com/forums/t958635-meanning-of-the-for-loop.html)

Rudra Banerjee 03-13-2013 04:41 PM

meanning of the for loop
 
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)

Lew Pitcher 03-13-2013 04:54 PM

Re: meanning of the for loop
 
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"


Mark Bluemel 03-13-2013 05:00 PM

Re: meanning of the for loop
 
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.




Eric Sosman 03-13-2013 05:03 PM

Re: meanning of the for loop
 
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
esosman@comcast-dot-net.invalid

Rudra Banerjee 03-13-2013 05:12 PM

Re: meanning of the for loop
 
On Wednesday, March 13, 2013 5:00:59 PM UTC, Mark Bluemel wrote:
>
> Your understanding is weak...
>

I agree. Thanks for helping

Rudra Banerjee 03-13-2013 05:13 PM

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



Keith Thompson 03-13-2013 05:16 PM

Re: meanning of the for loop
 
Rudra Banerjee <bnrj.rudra@gmail.com> 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) kst-u@mib.org <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"

Shao Miller 03-13-2013 07:34 PM

Re: meanning of the for loop
 
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

BartC 03-14-2013 11:48 AM

Re: meanning of the for loop
 
"Rudra Banerjee" <bnrj.rudra@gmail.com> wrote in message
news:7f6a60fc-29c0-43dd-ba01-af5d942b1dec@googlegroups.com...

> 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



All times are GMT. The time now is 04:24 PM.

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