What is the scope of a while and for loop?
while
for
For example, if I declared an object within the loop, what is its behavior and why?
Anything declared in the loop is scoped to that loop and cannot be accessed outside the curly braces. In fact, you don't even need a loop to create a new scope. You can do something like:
{ int x = 1; } //x cannot be accessed here.