ForVariableScopeBeyondLoop
DRAFT

Misconception:

The scope of a variable defined in a for loop header extends beyond that loop.

Incorrect

The scope of variables declared in a for loop header extends beyond the loop

Correct

The scope of variables declared in a for loop header is limited to the loop

Correction
Here is what's right.

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

Symptoms
How do you know your students might have this misconception?

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).

Stay up-to-date

Follow us on  twitter to hear about new misconceptions.