3

I'm reading code and I come across this code:

for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr)

How should I interpret it? Does the for loop have multiple increments? I've seen multiple conditions before, separated by comma, but I never saw multiple increments before.

Montao
  • 245
  • 1
  • 3
  • 12
  • That's not a multiple increment strictly though,former one is assignment, the latter is the incremental code strictly. – Am_I_Helpful Jun 03 '15 at 14:42
  • 1
    possible duplicate of [Is it possible to do multiple operations in increment part of for loop in C/C++?](http://stackoverflow.com/questions/19236658/is-it-possible-to-do-multiple-operations-in-increment-part-of-for-loop-in-c-c) – KevinDTimm Jun 03 '15 at 14:42
  • On a different note, I'd avoid for loops without an termination condition. You can always exit the loop with a break, but if you forget it, ... – onitake Jun 03 '15 at 14:45

6 Answers6

5

In this case, on every iteration of the loop, both statements are called. It appears to be some sort of linked-list. You can clearly see how this works if you write it as a while loop:

p = prevp->s.ptr;
while (true) {
    //Loop body here

    prevp = p;
    p = p->s.ptr;
}
Glenn Smith
  • 912
  • 1
  • 17
  • 37
2

The for loop has the following format:

for( <initialization>; <condition>; <recurring operation> ) ....

<initialization> is taking place before the loop is entered first time (if at all).
<recurring operation> is performed after each iteration.
<condition> is checked before each iteration.

In your case:
<initialization> = p = prevp->s.ptr - initializing p to some pointer value
<condition> = nothing. So the loop will run forever, unless there is a break statement or some other flow control expression breaking the loop encountered.
<recurring operation> = prevp = p, p = p->s.ptr. It is updating two variables here prevp and p, such that in the next iteration they both will have the new values. The coma operator is evaluated from left to right, so first prevp is updated, and next the p.

Eugene Sh.
  • 17,802
  • 8
  • 40
  • 61
1

Have a look at the comma operator. Basically, the increment part of the for consists of an expression with the comma operator. So your assumption is correct.

foxx1337
  • 1,859
  • 3
  • 19
  • 23
1

The formal syntax of for statement is:

for ( clause-1 ; expression-2 ; expression-3 ) statement

Any of clause-1, expression-2, and expression-3 can allow multiple expressions in them by using the comma operator.

In clause-1, you can initialize multiple variables.

for ( cp1 = s1, cp2 = s2; ...

In expression-3, you can change multiple variables.

for ( cp1 = s1, cp2 = s2; ...; ++cp1, ++cp2 )

In your case, expression-3 is prevp = p, p = p->s.ptr.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • `expression 2` allows only one expression. – haccks Jun 03 '15 at 14:56
  • 1
    @haccks, Not true. You can use the comma operator to have multiple expression there too. It will be pretty ugly code but the syntax allows it. See it working at http://ideone.com/3I0Vy2. – R Sahu Jun 03 '15 at 15:01
  • `i < 10, j < 20;` will ruin the purpose of using comma operator. – haccks Jun 03 '15 at 15:46
  • @haccks, I agree. That was just a quick and dirty hack to show that it is legal. – R Sahu Jun 03 '15 at 15:51
0

Yes, the two last expressions will be executed after each iteration of the loop.

rounak
  • 9,217
  • 3
  • 42
  • 59
0

In this statement -

for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) 

Actually there is no multiple increment. At the beginning of the for loop we see p = prev->s.ptr which is actually initialization of p.

Then the code placed in the for loop will be executed.

After that the condition portion is evaluated. Here there is no condition. So the loop continues unless it is break from inside the code block of the for-loop.

The the increment section of the for-loop is reached. Now previous point to p and p point will takes the next value.

Razib
  • 10,965
  • 11
  • 53
  • 80