FusedQyou
scope of a for loop
Finally, scope is anything in
{}
brackets. You should understand that scope is not a strict thing and {}
is valid anywhere in the code, not just if-statements or things like that. You can nest {}
infinitely and it would technically make a new scope each time. It's just most commonly used in for-loops/if-statements/while-statements because they are tied to execute a single line otherwise and this way you execute a whole scope.
I personally use scopes if I have common names for variables and I define them multiple times, but they vary per context. It's nice being able to wrap them in {}
so the scope gets rid of the name so I can just redefine them later.171 replies
scope of a for loop
This is fine, a for loop is not required to define a variable to loop, but it does require the semicolons to be defined. As jimmacle mentioned this makes
for(;;)
valid but it would be the same as an infinite while loop. Depends on use case171 replies