What is the scope of a 'while' and 'for' loop?

后端 未结 10 1653
有刺的猬
有刺的猬 2020-11-30 06:37

What is the scope of a while and for loop?

For example, if I declared an object within the loop, what is its behavior and why?

10条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 07:14

    Just wanted to add that variables declared in the for or while loop are also scoped within the loop. For example:

    for (int index = 0; index < SOME_MAX; ++index)
    {
        ...
    }
    
    // index is out of scope here.
    

提交回复
热议问题