Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
253 views
Welcome To Ask or Share your Answers For Others

1 Answer

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...