Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The x-Done Loop

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-01-07 7:58

This is a construct that is very useful to avoid either a redundant check or a goto. This...
for(a;b;c) {
 expr1;
 if(d) {
  expr4;
  goto done;
 }
 expr2;
}
 expr3;
done:

...becomes...
for(a;b;c) {
 expr1;
 if(d) {
  expr4;
  break;
 }
 expr2;
} done {
 expr3;
}


where the intent is for statements in a done clause immediately following a loop to be executed if and only if the loop was exited via its termination condition being reached. This also generalises for while() and do-while, here using some common examples.

while(*j) {
 if(*j++ == k) {
  u = v + k;
  break;
 }
} done
 u = v - k;


do {
 if(f(a[i]))
  break;
} while(a[i++]) done {
 g();
}


Discuss.

Name: 27,28 2013-01-10 8:42

>>54,55
How so? I'd say it's far cleaner to write a separate function like 'searchuranus' then branch depending on its return value, rather than using what you've proposed. Performing an extra comparison on a null pointer really isn't a huge issue, especially with high level languages where clarity and the use of idiomatic forms is far more important than unnoticeable performance gains. If you run a large program through a profiler and find that it performs poorly, chances are it's because an inefficient data structure was used, rather than using two comparisons where one would have been sufficient.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List