3
for (; cnt--; dp += sz)
{
        pair_sanitize_struct(rec_id, ctx->api_mode, dp, FALSE);
}

Could some one explain how this for loop works? It belongs to a cpp file. I dont understand the condition in the for loop and how it is being checked. (The function is being invoked)

7 Answers7

2

The general form of for statement looks like this:

for (init-statement; condition; expression)
    statement

init-statement is used to initialize or assign a starting value that is modified over the course of the loop. condition serves as the loop control. As long as condition evaluates as true, statement is executed. expression is evaluated for each iteration only if condition is true

Back to your code:

for (; cnt--; dp += sz)

init-statement here is a null statement that does nothing. condition is cnt-- which evaluates its value as cnt then decrements 1. If cnt is non-zero, condition is true, if cnt is zero, condition is false.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    init-statement executes only once before the start of the loop. condition is checked for each iteration and the expression is evaluated for each iteration only if the condition is true – Jack Sep 25 '13 at 06:18
  • 1
    @Jack Or rather, the condition is checked before each lap in the loop and the 3rd expression after each lap. The for loop is executed as: `condition (stop if false) -> loop body -> 3rd expression`. – Lundin Sep 25 '13 at 07:42
1

The condition is being interpreted as a true or false scenario.

If it's 0, then it will be false, else true.

nemasu
  • 426
  • 2
  • 10
1

This is equivalent to the following code -

for(; cnt-->0; dp += sz);

Because as long as a value is not equal to 0, it is considered to be true.

David says Reinstate Monica
  • 19,209
  • 22
  • 79
  • 122
  • I respect the answer as a valid one. But certainly using --> is not common c (considering some one just learning the language as OP), probably not known to many programmers and is certainly not recommended for a portable and readable in the above situation. – fkl Sep 25 '13 at 06:18
  • @fayyazkl Are you referring to this: http://stackoverflow.com/questions/1642028/what-is-the-name-of-this-operator? – David says Reinstate Monica Sep 25 '13 at 06:19
  • yes certainly. Without the space, most of the people (including myself when i saw this first time way back), would think if it as a new operator of some kind – fkl Sep 25 '13 at 06:24
0

In c++, the value for a condition being true or false is determined by being non 0 (true) or 0 (false).

The above loop would continue iterating as long as cnt is NOT 0. It will terminate when cnt becomes 0.

UPDATE:

To clear an important point here, it is the value 0 that terminates the loop. If for some reason, cnt already starts with a negative value, the loop will never terminate

fkl
  • 5,412
  • 4
  • 28
  • 68
  • Actually, because the condition is a post-increment expression, it terminates when `cnt` becomes `-1` (or underflows if it's unsigned). – Benjamin Lindley Sep 25 '13 at 06:11
  • 1
    @BenjaminLindley No, `cnt` will be `-1` *after* the loop, but it's still the value `0` that causes the loop to end. – Some programmer dude Sep 25 '13 at 06:12
  • @JoachimPileborg: That is exactly in line with my statement. – Benjamin Lindley Sep 25 '13 at 06:14
  • I was about to write that @BenjaminLindley. At least your wording sounds confusing. If the loop does not hit 0, it will never terminate. Negative value is as good as positive. I know you didn't mean that but just elaborating since i felt the same at first from your statement. – fkl Sep 25 '13 at 06:16
  • I really don't see how my wording is confusing. Your answer says that the loop will terminate when `cnt` becomes `0`. But this is clearly not true. When `cnt` becomes `0`, the loop will execute one more time. Then `cnt` will become `-1`, at which time, the loop terminates, because the expression (`cnt--`) which brought `cnt` to `-1`, evaluates to `0`. Ergo, the loop terminates when `cnt` becomes `-1`. – Benjamin Lindley Sep 25 '13 at 06:22
  • I believe the focus was on the terminating condition (whether being 0 or non zero) rather than post decrement and one more loop iteration. IMHO, you are sounding like -1 would be the terminating condition, which is not true. It only reaches -1 AFTER the loop. Following should illustrate that int i=0; for(; i--;) printf("%d\n", i);. It will not print any thing. So 0 is what terminates the loop. The post dcrement comes after the evaluation so i don't see why my statement is wrong in any way – fkl Sep 25 '13 at 06:33
  • For those who tried editing and rejected, NON 0 is true (i.e. positive or negative both inclusive) and 0 is false, so the statement holds. – fkl Sep 25 '13 at 06:41
  • Lastly, @BenjaminLindley your above statement respectfully is completely wrong. IT DOES TERMINATE when value is 0. Please double check before commenting. Thank you. – fkl Sep 25 '13 at 06:52
  • Perhaps this will demonstrate my point: http://ideone.com/iLvS2M -- And by the way, it is unspecified, as far as the standard is concerned, whether the evaluation happens before or after the incrementation, and there is no way you can check without invoking undefined behavior. – Benjamin Lindley Sep 25 '13 at 06:53
  • Also, I didn't say that the loop doesn't terminate when `cnt` is 0. I said it doesn't terminate when `cnt` "becomes" 0. "is" and "becomes" are two different words with completely different meanings. If you meant "is", you should have said "is". But you didn't say "is", you said "becomes", which is clearly wrong. – Benjamin Lindley Sep 25 '13 at 07:04
0

Remember that normal integers can be used as boolean values as well, where zero is false and everything non-zero is true.

This means that the loop will continue until cnt is zero, and then the loop will end. However that's not the whole story, since the post-decrement operator is used the value of cnt after the loop have ended will be -1.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

It is similar to

while(cnt--)
{
        pair_sanitize_struct(rec_id, ctx->api_mode, dp, FALSE);
        dp += sz;
}

hope this is helpful.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Amit Chauhan
  • 6,151
  • 2
  • 24
  • 37
0

So syntax for for loop is

 for (<initialization(optional)>; <condition(Optional)>; <increment(Optional)>)
 {
    ...
 }

Say for cnt is 2 your loop works as follows,

  for(; cnt--; dp+=size)
  {
      ...
  }

Execution flow is,

 1. initialization statement will be executed once. Since you dont have one nothing will be executed

 2. Next condition statement will be executed. In your case cnt-- which result in cnt value is considered as condition result. So, if cnt is 2 then value 2 is considered as condition result. Hence all non-zero are considered as TRUE and zero is considered as FALSE. After evaluating to TRUE it decrements cnt by 1

 3. Once the condition results in TRUE then it executes the statement part say,  pair_sanitize_struct(rec_id, ctx->api_mode, dp, FALSE);

 4. At the last it executes the increment statement of for loop,in you case it is dp-=size;

 5. It executes from 2 till condition evaluated to ZERO ie FALSE it comes out of loop.
Saravanan
  • 1,270
  • 1
  • 8
  • 15