2

I have a strange syntax of for loop here :

for(--index ; index>=0 ; --index)     // (--index) instead of (forInit) 
{
    //code
}

I know for syntax is like this :

for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

How do we have --index instead ForIniti in the loop. I thought that we can only initialise a variable at that place or can leave it blank like this :

for( ; index>=0 ; index--)
    {
      //code
    }

I haven't been introduced to this syntax before. Please explain why this is syntactically correct.

  • 1
    Just because you have `[ForInit]` at the beginning, doesn't mean you can't do things other than initialize a variable. – Arc676 Jun 16 '16 at 09:39
  • so it does it mean I can have any other expression instead of forinit –  Jun 16 '16 at 09:41
  • You can do whatever it is you need to do before your loop (good practice such as readability and other limitations which I will not list still apply). – Arc676 Jun 16 '16 at 09:42
  • 1
    Think at it as: _something to be done before start looping_ – LPs Jun 16 '16 at 09:42
  • 2
    The initialization expression in a `for` loop can contain any expression, including comma expressions. Normally it's some kind of assignment statement, but it doesn't have to be. C99 also allows variables to be declared. – Tom Karzes Jun 16 '16 at 09:42
  • even this is allowed `for( ; ; )` and regularly used, in fact (-: – user3078414 Jun 16 '16 at 09:44
  • A shorter (the shortest, actually) version equivalent of your loop is `while(--index >= 0){ /*code*/ }` – CiaPan Jun 16 '16 at 09:58

6 Answers6

3

The definitions of for statement in N1570 6.8.5 Iteration statements is:

for ( expression opt ; expression opt ; expression opt ) statement
for ( declaration expression opt ; expression opt ) statement

Arbitrary expressions are allowed to all of the three fields, so of course expression --index is allowed, too.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

The [ForInit] thing is an expression. Any expression. So are the others, which makes the for loop very powerful.

One semi-common thing to do is to use it to step through a linked list:

struct node {
  struct node *next;
  void *data;
} *head, *iter;

for(iter = head; iter != NULL; iter = iter->next)
{
}

This uses a (perhaps strange-looking, but perfectly fine) [ForUpdate] part, that follows the next link instead of doing arithmetic on a counter.

unwind
  • 391,730
  • 64
  • 469
  • 606
1

In for init you can initialize a varible like : index = k, and also index = index +k or index = index -k, In you example the initialization is simple index = index -1.

Example:

index= 10; 

for(--index ; index>=0 ; --index)    
{    
}

Index starts with a value of 9 and decreased untill 0.

1

It is nothing but

for(index = index - 1 ; index>=0 ; --index){
    //code
}

I do not know where you found the mentioned for syntax but, strictly speaking, you just need to include an expression there(probably empty).

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
2rd_7
  • 462
  • 1
  • 3
  • 15
0

In this for syntax:

for ( [ForInit] ; [Expression] ; [ForUpdate] ) Statement

ForInit is the expression which gets executed when the loop starts, hence, executed only once for a loop.

Expression is the conditional expression, loops runs only as long as it evaluates to True or 1 or any other non-zero number.

ForUpdate is the expression executed after each loop body execution.

This statement is not correct:

I thought that we can only initialise a variable at that place or can leave it blank

You can have any expression at this (ForInit) place, only it will get executed once at the start of the loop.

Why this is syntactically correct?

for( ; index>=0 ; index--) {
      //code
}

It is absolutely correct because in for loop, all the three parameters are optional. The below loop is also correct but it is an infinite loop:

for( ; ; ){
}
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
0

The C standard is lax and therefore allows you to put any kind of crap in either of the 3 for loop expressions (though the middle one must evaluate to arithmetic type, it can't be a void expression). But this doesn't mean that you should run off and put any kind of crap there, as done in this badly-written code.

You should always write for loops like this:

  • The 1st expression should only be concerned with declaration and/or initialization of the loop iterator.
  • The 2nd expression should only contain the loop condition.
  • The 3rd expression should only be concerned with changing the loop iterator.

Anything else but the above de facto standard is very bad programming practice. You should always avoid having any form of unrelated side-effects in any of the 3 expressions if possible.

Also try to keep the loop iterator local scope to the loop. In this case you could perhaps declare an iterator i ("i" stands for iterator) and assign the value of the index variable to it int i=index - 1;.

Lundin
  • 195,001
  • 40
  • 254
  • 396