I was for quite some time under the impression that a for
loop could exist solely in the following format:
for (INITIALIZER; STOP CONDITION; INC(DEC)REMENTER)
{
CODE
}
This is, however, most definitely not the case; take a look at this JavaScript implementation of the Fisher-Yates Shuffle:
shuffle = function(o)
{
for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
This little snippet completely blows my mind; how in the world is so much going on inside a simple for loop declaration? I mean... it doesn't even open a brace! All of the magic is being done right there inside the for
statement. It'd be absolutely wonderful if somebody could provide a relatively thorough explanation as to how in the world this voodoo is doing what it does. Much appreciated in advance.