views:

259

answers:

8

I have been looking through some code and I have seen several examples where the first element of a for cycle is omitted.

An example:

for ( ; hole*2 <= currentSize; hole = child)

What does this mean?

Thanks.

+1  A: 

It means that the initial value of hole was set before we got to the loop

John Knoeller
+15  A: 

It just means that the user chose not to set a variable to their own starting value.

for(int i = 0; i < x; i++)

is equivalent to...

int i = 0;
for( ; i < x; i++)

EDIT (in response to comments): These aren't exactly equivalent. the scope of the variable i is different.

Sometimes the latter is used to break up the code. You can also drop out the third statement if your indexing variable is modified within the for loop itself...

int i = 0;
for(; i < x;)
{
...
i++
...
}

And if you drop out the second statement then you have an infinite loop.

for(;;)
{
runs indefinitely
}
Pace
Declaring i inside the for loop pre-initialization isn't *quite* equivalent to declaring it just before the loop. In the former case, its scope is the body of the for statement -- i.e., you can't access it after the for statement's body closes. In the latter case, the variable i can still be accessed after the for loop body.
Edward Loper
While you *can* use `for(;i<x;)`, it's stylistically better to go with the smipler and easier-to-read `while(i<x)`.
Edward Loper
Actually those aren't equivalent. "i" is declared only for that scope when declared in the for keyword.
I also like to go with smipler code. Also, it should be as Redditable as possible. :)
dar7yl
Actually, you don't need to set any variable in the first expression of a `for` statement. Try this: `for (std::cout<<"first\n"; false;);`. As Edward said in his comment, it is a pre-initialization step.
Thomas Matthews
+1  A: 

That means loop control variable is initialized before the for loop .

For C  code,

int i=0;
for( ; i <10 ; i++) { } //since it does not allow variable declaration in loop 

For C++  code,

for(int i=0 ; i <10 ; i++) { }  
Ashish
C99 allows you to use the "C++" form, so it is no longer necessary to declare the variable outside the for statement. However, in both C and C++, a variable declared inside the for statement goes out of scope after that statement. If you want to use the variable after the for statement is finished, the for(;i<10;i++) syntax is necessary.
Chinmay Kanchi
+1  A: 

You could omit any of the parameters of a for loop. ie: for(;;) {} is about the same as while(true) {}

Alan Alvarez
+7  A: 

The for construct is basically ( pre-loop initialisation; loop termination test; end of loop iteration), so this just means there is no initialisation of anything in this for loop.

You could refactor any for loop thusly:

pre-loop initialisation
while (loop termination test) {
...
end of loop iteration
}
cyborg
+1  A: 

It means that the initial value of hole was set before we got to the loop.

Looks like a list traversal of some kind.

Michal Sznajder
+1  A: 

Suppose you wanted to

for (hole=1 ; hole*2 <= currentSize; hole = child)

But the value of hole just before the for loop was already 1, then you can slip this initilization part of the loop:

/* value of hole now is 1.*/
for ( ; hole*2 <= currentSize; hole = child)
codaddict
+1  A: 

Some people have been getting it wrong so I just wanted to clear it up.

int i = 0; for (; i < 10; i++)

is not the same as

for (int i = 0; i < 10; i++)

Variables declared inside the for keyword are only valid in that scope.

To put it simply.

Valid ("i" was declared outside of the loops scope)

int i = 0; for (; i < 10; i++) { //Code } std::cout << i;

InValid ("i" does not exist outside the loop scope)

for (int i = 0; i < 10; i++) { //Code } std::cout << i;

(Can someone fix the code tags? I can't get them to work.)