user2813853
user2813853

Reputation:

For loop condition (cpp)

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)

Upvotes: 3

Views: 475

Answers (7)

fkl
fkl

Reputation: 5535

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

Upvotes: 0

Saravanan
Saravanan

Reputation: 1440

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.

Upvotes: 0

Amit Chauhan
Amit Chauhan

Reputation: 6879

It is similar to

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

hope this is helpful.

Upvotes: 0

Yu Hao
Yu Hao

Reputation: 122383

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.

Upvotes: 2

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.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409176

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.

Upvotes: 0

nemasu
nemasu

Reputation: 426

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

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

Upvotes: 1

Related Questions