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

后端 未结 8 1119
猫巷女王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:40

    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;
    

提交回复
热议问题