Strange behaviour of while loop !!

K

karthikbalaguru

Hi,

I have a query on the behaviour while loop
based on code snippet given below -

int main(void)
{

int x = 0;
int y = 1;
while (y=10,x++)
{
printf("x = %d\n",x);
x++;
}
printf("y = %d\n", y);
return 0;
}

This above code prints the output as follows
y = 10

But, if the while is written like ' while (y=10) ', then it
becomes as while(1) and keeps printing the value of
x continuously .
Hence, shouldn't while(b=10,a++) should also become
as while(1) and keep printing the value of x continuously ?
But, how does it give output as 'y = 10' .
Any ideas ?

Thx in advans,
Karthik Balaguru
 
D

Daniel Kraft

karthikbalaguru said:
Hi,

I have a query on the behaviour while loop
based on code snippet given below -

int main(void)
{

int x = 0;
int y = 1;
while (y=10,x++)
{
printf("x = %d\n",x);
x++;
}
printf("y = %d\n", y);
return 0;
}

Hence, shouldn't while(b=10,a++) should also become
as while(1) and keep printing the value of x continuously ?
But, how does it give output as 'y = 10' .
Any ideas ?

The value of "y=10,x++" is the value of "x++" which is 0 and thus the
loop body is never executed. HOwever, y=10 gets done before of that, so
the program prints "y = 10" (but nothing else).

Hope this helps,
Daniel
 
J

Jens Thoms Toerring

karthikbalaguru said:
I have a query on the behaviour while loop
based on code snippet given below -
int main(void)
{
int x = 0;
int y = 1;
while (y=10,x++)
{
printf("x = %d\n",x);
x++;
}
printf("y = %d\n", y);
return 0;
}
This above code prints the output as follows
y = 10
But, if the while is written like ' while (y=10) ', then it
becomes as while(1) and keeps printing the value of
x continuously.
Hence, shouldn't while(b=10,a++)

I guess you meant (y=10,x++) here, right?
should also become
as while(1) and keep printing the value of x continuously ?

No.

while ( y = 10, x++ )

first sets y to 10, then, because of the comma operator,
forgets about the value of y, determines what x is set
to (which is 0) and increments x.

That might become clearer if you consider

int a = 0;
int b = a++;

This will set b to 0 and not to 1 since a is only incre-
mented after its value is evaluated (becausse the '++'
operator comes after a) for what to assign to b.

The result of the expression '( y = 10, x++ )' is thus
0 and has the side effect of setting y to 10 and x to 1.
Thus the body of the while loop is never executed. All
what then happens is that the value of y is printed
once, which you has set to 10 in the while loop con-
dition.

If you would like to have an infinite (or at least a
rather long) loop you would have to use e.g.

while ( y = 10, ++x )

since you now would increment x before its value is
considered for the purpose of determining if the
loops body has to be executed.

Regards, Jens
 
K

karthikbalaguru

I guess you meant (y=10,x++) here, right?


No.

while ( y = 10, x++ )

first sets y to 10, then, because of the comma operator,
forgets about the value of y, determines what x is set
to (which is 0) and increments x.

Thx for the clarification.
But, what is the advantage/use of writing a while
loop in a way that it forgets about the first
argument and takes the second argument for
evaluation ? Is it some kind of optimization ?

Where can i use such kind of while loop with
more than 2 arguments ?

Any ideas ?

Thx in advans,
Karthik Balaguru
 
I

Ian Collins

karthikbalaguru said:
Thx for the clarification.
But, what is the advantage/use of writing a while
loop in a way that it forgets about the first
argument and takes the second argument for
evaluation ? Is it some kind of optimization ?
No, it's a form of obfuscation!
Where can i use such kind of while loop with
more than 2 arguments ?

You can't, There is only one argument, the last expression in the comma
operator. The comma operator is evaluated left to right, so the first
expression (y=10) forms no part in the loop control.
 
B

Bartc

karthikbalaguru said:
Thx for the clarification.
But, what is the advantage/use of writing a while
loop in a way that it forgets about the first
argument and takes the second argument for
evaluation ? Is it some kind of optimization ?

Where can i use such kind of while loop with
more than 2 arguments ?

The while above is similar to:
while (1) {
y=10;
if (x++) break;
....
}

It doesn't 'forget' the y=10 term. And the while loop doesn't really have
two arguments.
 
K

karthikbalaguru

No, it's a form of obfuscation!


You can't,  There is only one argument, the last expression in the comma
operator.  The comma operator is evaluated left to right, so the first
expression (y=10) forms no part in the loop control.

hmm. ok.
If there is no use of such kind comma operator based
while loops , then why does C continue to support it ?

Will it be removed in the next release ?
Any ideas ?

Thx in advans,
Karthik Balaguru
 
K

Keith Thompson

karthikbalaguru said:
[snip]
But, what is the advantage/use of writing a while
loop in a way that it forgets about the first
argument and takes the second argument for
evaluation ? Is it some kind of optimization ?

Where can i use such kind of while loop with
more than 2 arguments ?

y=1 and x++ are not separate arguments. The syntax for a while loop
is simply

while ( expression ) statement

In this case, the expression is "y=10,x++", where the comma is a comma
operator. As you'll see when you look this up in your C reference, a
comma operator takes two arguments; it evaluates the left operand (and
discards any result), and then evaluates the right operand and yields
its result. It usually doesn't make much sense to use a comma
operator unless, as in this case, the left operand has a side effect.

In your snippet, the condition causes y to be assigned the value 10 at
the top of each iteration of the loop. Since the loop doesn't modify
the value of y, it would make more sense to do the assignment just
once:

...
y = 10;
while (x++)
{
...
}

There probably are cases where it actually makes sense to use a comma
operator in a while loop condition, but I can't think of any plausible
ones off the top of my head.
 
I

Ian Collins

karthikbalaguru said:
hmm. ok.
If there is no use of such kind comma operator based
while loops , then why does C continue to support it ?
As Keith said, The syntax for a while loop is

while ( expression ) statement

a comma operator is an expression.
Will it be removed in the next release ?

Remove the comma operator? No way.
 
K

Keith Thompson

Ian Collins said:
No, it's more like

int y = 10;
while (x++)
{
....
}

Yes, but only because the body of the loop doesn't modify y. The
assignment "y = 10" is executed on each iteration of the loop.
 
I

Ian Collins

Keith said:
Yes, but only because the body of the loop doesn't modify y. The
assignment "y = 10" is executed on each iteration of the loop.
Good point, I overlooked the full expression in favour of the result.
 
T

Tim Rentsch

Keith Thompson said:
[stuff about comma operator in a while condition]

There probably are cases where it actually makes sense to use a comma
operator in a while loop condition, but I can't think of any plausible
ones off the top of my head.

I find

while( c = getchar(), c != EOF ){ ... }

somewhat more aesthetically pleasing, at least along some axes, than
the other well-known form which wraps the assignment expression in
parentheses and uses the parenthesized expression as a comparand
against EOF. I'm sure others would offer different opinions;
personally, I like the way the comma operator makes the side
effect of calling getchar() stand out here.
 
K

Kenny McCormack

Keith Thompson said:
There probably are cases where it actually makes sense to use a comma
operator in a while loop condition, but I can't think of any plausible
ones off the top of my head.

Something like:

while (i+=2,j) { ... }

to step through every other element of j. And for some reason, you
don't want to use the equivalent for () statement. Shouldn't be too
hard to construct such a situation. Also, there might be some stylistic
reason why you don't want to use j[i+=2]. Maybe a company policy
against doing things like that (yes, stranger things have happened).
 
D

David Thompson

There probably are cases where it actually makes sense to use a comma
operator in a while loop condition, but I can't think of any plausible
ones off the top of my head.

The (fairly few) good cases I can remember are where I want to iterate
over some set of foos and there is an available (not changeable)
function that _almost_ does get_next_foo but either:

- needs some setup before every call:
state = initial; /* often = get_foo_initial_state (...) */
while( junk=42, get_junky_foo (&state, &junk, &foo) ) ... foo ...

- or doesn't indicate end by the return value:
state = initial; /* or get_foo_initial_state (&state) */
while( foo=maybe_get_foo (&state, &valid), valid ) ... foo ...

Yes, both of these could be done as N-and-a-smidge instead.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top