Re:Tom Duff: Reading Code From Top to Bottom (1999)
by barrkel on Sunday June 05, 2011 @01:25AM
(#2621388)
On the topic of loops which end in a break ("Most egregious is the loop whose last statement is break;!"): this can be useful for avoiding contorted control flow when you have a switch statement inside the loop. Instead of coding:
cont = 1;
while (cont)
{
foo = bar();
switch (foo)
{
case 0:
cont = 0;
break;
default:
// other work
break;
}
}
You can do this instead:
for (;;)
{
foo = bar();
switch (foo)
{
case 0:
break;
default:
// other work
continue;
}
break;
}
IMO the idiom, once you're used to it, reads better.
(Yes, I know you can reduce the lines further e.g. putting foo=bar() into the for statement, but it's the multi-level break I'm focused on here.)
(Yes, I know you can reduce the lines further e.g. putting foo=bar() into the for statement, but it's the multi-level break I'm focused on here.)