DRAFT
Observed
The scope of variables declared in a for loop header extends beyond the loop
The scope of variables declared in a for loop header is limited to the loop
A variable declared in the for loop header is only visible within the loop header and body, but not outside the loop. The scope of that variable is the loop.
for (int i=0; i<n; i++) {
// variable i is in scope here
}
// variable i is NOT in scope here
This misconception shows up when students need to write multiple loops in a method, like this:
for (int i=0; i<n; i++) {
// work with i
}
for (int j=0; j<n; j++) {
// work with j
}
Students with that misconception use a different variable name for the second loop (even if the purpose of that variable is the same).
Follow us on twitter to hear about new misconceptions.