tags:

views:

154

answers:

4
+2  Q: 

c while loop entry

hi somebody please explain me why

int i=0,j=10;
    while(j>0,i++){
    printf("%d%d",i,j);
    j--;
}

will not work and

int i=0,j=10;
while(i++,j>0){
    printf("%d%d",i,j);
    j--;
}

works.

Also please tell me why

int i=0,j=10;
while(j>0,++i){
    printf("%d%d",i,j);
    j--;
}

gives an infinite loop ?

thanks and regards

harsha

+16  A: 

In your while-loop condition you use comma operator, which evaluates its parameters and returns the second one. So in your examples:

while(j>0,i++) - returns i before it gets incremented; 
                 that is 0, so loop won't execute at all 

while(i++,j>0) - returns (j > 0) - runs as expected

while(j>0,++i) - returns i - will run until i overflows  max int value
Vladimir
The first one exits immediately - it's different from #3. Otherwise correct.
Carl Norum
Yes, you're right, fixed that
Vladimir
Note that in the last example, at the point where `++i` overflows the loop doesn't *necessarily* exit - the behaviour becomes undefined.
caf
+4  A: 

Read up on the C comma operator. Basically it winds down to the fact that the comma operator returns the result of whatever is on the right side of the comma - so in your first example, i++ returns 0, and the loop ends. In the third case, ++i is never 0 (at least not for a long time) so you get the infinite loop. The middle case is ok, since the result of j>0 is returned from the comma operator, and your loop works as expected.

Carl Norum
I see how this works, but would this usage be considered a "good practice" in c? Seems a bit obscure.
Robert Harvey
Definitely not. Who in the right mind use a comma operator in the while condition?
Yuji
The page you linked to suggests that the most common usage is in a `for` loop, although the examples the author gives are even more obscure than the `while` loop shown here. It's difficult to tell which statement is actually being returned in each part of the `for` loop.
Robert Harvey
Edited to change link.
Carl Norum
caf
Thanks, it's good to learn things from the experts :)
Yuji
+1  A: 

You're using the comma operator. The result of the comma operator is the second sub-expression. So j>0,i++ evaluates to i++. i++ is initially 0, so the loop never executes.

Likewise, j>0,++i evaluates to ++i, which will be non-zero until you overflow, so it appears to loop forever (though really just for a long time).

i++,j>0 works because the last-subexpression is j>0, which is the actual condition that you want. Note that even though the comma operator throws away the result of the first expression (i++, in this case) it still evaluates that sub-expression, and so you still get the side-effect (incrementing i).

Laurence Gonsalves
A: 

In these cases, you have expressions that include a comma operator. The comma operator evaluates its left operand, then its right operand. The result is that of the right operand.

For the moment, let's consider only your last question:

int i=0,j=10;
while(j>0,++i){
    printf("%d%d",i,j);
    j--;
}

This should not really be an infinite loop, though it may run just a bit longer than expected. Since the result of the comma operator is the result of the right operand, this is waiting for ++i to become 0. Since it's starting at 0, it's going to cycle though all possible values of an int before it terminates. With a 16-bit int, that would probably take a few minutes. With a 32-bit int, it's going to take quite a bit longer. With a 64-bit int, it would be an infinite loop for any practical purpose -- I'm pretty sure I'll die long before it could hope to finish at any rate...

Jerry Coffin
It will run at least until `i` is equal to `INT_MAX` - at that point, `++i` overflows, which results in undefined behaviour. This means that *anything* is possible, including an infinite loop (`++i` might well leave `i` unchanged!).
caf
@caf: In theory, quite true. In fact, you're unlikely to see anything like that -- while there is hardware to provide saturating integers, I've never seen anybody use it for a normal `int`. Given the degree of similarity required between signed and unsigned variants, and the requirement that `unsigned` wrap modulo 2^N-1, I'm not sure there's even hardware that *could* do so, even though it's theoretically possible.
Jerry Coffin
Well, the saturating functionality would be an attribute of the "ADD" operation - you could imagine an architecture that provided both `Add-With-Saturate` and `Add-With-Wrap` instructions, with a C compiler using the former for signed addition and the latter for unsigned. Much more likely though is a trap on overflow - if the trap is handled by skipping the trapping instruction, then it would also have the saturating effect (though a program abort is much more likely).
caf