Joanna, I'm taking the liberty of mis-quoting you by swapping your sentences round...
I often use the index inside the for loop for various things such as case statements...
Which is why the control variable has an associated name, which has been accepted practice since e.g. early FORTRAN:
do 20 i = 10, 1, -2
write(*,*) 'i =', i
20 continue
There are other ways of doing it e.g. by iterating over an ordered collection, but if you want your control variable to be accessible to e.g. index into an array or be an operand in an arithmetic expression having it as a variable is pretty standard: I think that pretty much all of us who've done some "under the hood" compiler work have tried alterbatives and found them lacking.
I didn’t know that a for loop was a cpu register I always imagined that it was just the variable being incremented automatically.
Right. I don't want to sound condescending here, but you have said that you know little if anything about compilers so let's see whether we can throw some light on things.
When you're inside a function (aka procedure in Pascal terms) variables get stored on the stack. If you consider a simple CPU with only a couple of general-purpose registers you should be able to see that the value of any variable is going to be continually shuffled back and forth between memory and register, and allowing for the gross disparity between register and RAM speed that gets to be inefficient. A relatively recent compiler looks at the control and data flows, and if a value in a register isn't needed then it won't be written back to memory.
So Wirth's dictum that the control variable has no defined value on completion of the loop doesn't necessarily mean that it /has/ to be stored in a register and never written out to a variable in memory, but it does permit the compiler to drop the value of the control variable at the end of the loop without saving it.
Whether a particular compiler, targeting a particular architecture with some number of registers, chooses to avail itself of that optimisation is an implementation issue. But it is a language definition issue that it /can/ use that optimisation, and that programmers should assume nothing else.
So assuming that the control variable is i, the value- in memory- of i on conclusion of the loop depends on whether it has ever been "spilled" from a register to memory, and that depends on whether calculations in the loop were sufficiently complex that the compiler decided that it was more efficient to spill it (and reuse the register for something else) than to keep it in a register. From the user's POV that is not easily predictable, and Wirth's language definition explicitly spares the user from making that decision.
Hope that helps.
MarkMLl
Edited: removed a second FORTRAN example which didn't make sense. Haven't touched that stuff since the 70s...