small bug fix for loop function

Hi, Andrew Gerrand, thanks for your speech and demo code.
But I think there mistyping some variable in your loop function, and here is the fix.
Thanks again.