for(; max != 0 ; max/=10, pow10*=10)
Pulled this exerpt from the wiki page on “Radix sort.” I’ve not seen a loop that starts with a semicolon like that before. The compiler didn’t catch it so I’m assuming it’s legal. Can anyone explain?
Also, as I’ve only written fairly simple loops I didn’t realize that you could make multiple assignments (right word?) like “max/=10, pow10*=10” at the end of the for() statement … Is there any limit to this? Bad form?
Thanks guys! (and pardon my potentially wrong vocabulary…it’s early, I need more coffee…)
It is perfectly legal. This is a
forloop with an empty initialization block.The most typical
forloop looks something like this:This is (almost) equivalent to the following:
(Except that in the first case the scope of
iis limited to within the loop, while in the second case,iis still visible after the loop, as its scope is the enclosing block.)For loops have three blocks, separated by semicolons: initialization, loop condition and loop increment. Any or all of these can be empty. So this is legal too (resulting in an endless loop):