What does it mean when the first “for” parameter is blank?

后端 未结 8 1053
猫巷女王i
猫巷女王i 2020-12-20 19:27

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          


        
相关标签:
8条回答
  • 2020-12-20 19:53

    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
    }
    
    0 讨论(0)
  • 2020-12-20 19:54

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

    Looks like a list traversal of some kind.

    0 讨论(0)
提交回复
热议问题