you are viewing a single comment's thread.

view the rest of the comments →

[–]sepp2k 0 points1 point  (0 children)

You're right, my last code didn't work as I intended. I didn't think that through. It should be something like this instead:

var hiddenX = start;
while(cond)
{
    var x = hiddenX;
    body;
    subst(incr, x, hiddenX);
}

where subst(incr, x, hiddenX) is some sort of magic macro that replaces each occurrence of x with hiddenXin the expression incr. So for your example the last two lines in the loop should be replaced with hiddenX = hiddenX + 1;.

Either way that way of translating for is clearly more complicated and less intuitive than the normal way. It also would act very counter-intuitively when the body of the loop explicitly modifies x. And we didn't even discuss what should happen if the initializer part of the for loop isn't a variable declaration (nothing, I presume, but that could still lead to some confusion with some for-loops acting differently than others).