Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > meanning of the for loop

Reply
Thread Tools

meanning of the for loop

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      03-13-2013
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)
 
Reply With Quote
 
 
 
 
Lew Pitcher
Guest
Posts: n/a
 
      03-13-2013
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"

 
Reply With Quote
 
 
 
 
Mark Bluemel
Guest
Posts: n/a
 
      03-13-2013
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.



 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      03-13-2013
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      03-13-2013
On Wednesday, March 13, 2013 5:00:59 PM UTC, Mark Bluemel wrote:
>
> Your understanding is weak...
>

I agree. Thanks for helping
 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      03-13-2013
On Wednesday, March 13, 2013 5:03:51 PM UTC, Eric Sosman wrote:
Thanks for the detailed explanation.


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-13-2013
Rudra Banerjee <(E-Mail Removed)> 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) (E-Mail Removed) <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"
 
Reply With Quote
 
Shao Miller
Guest
Posts: n/a
 
      03-13-2013
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
 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      03-14-2013
"Rudra Banerjee" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
The $a have any special meanning ? sonet Perl Misc 4 07-02-2007 05:20 AM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while loop in a while loop Steven Java 5 03-30-2005 09:19 PM
Loop the loop... =?Utf-8?B?VGltOjouLg==?= ASP .Net 2 02-16-2005 12:21 PM



Advertisments