Monday 28 November 2005

do { break; } while (0);

I just came up with this C construc, using break to jump out of a block;

do {
  if (! seems_suitable(a,b)) break;
  c=check_even_harder(a,b,);
  if (! c) break;
    be_tricky();
  return;
  } while (0);

do_it_the_old_way(a,b);


I was adding a code block before an existing block whose purpose was to filter out certain conditions to handle especially, and trying to avoid lots of nested if/else blocks. Without this foul trick the code would read:

if (seems_suitable(a,b)) {
  c=check_even_harder(a,b,);
  if (c) {
    be_tricky();
    return;
  }
}

do_it_the_old_way(a,b);


it works for me anyway .

2 comments:

  1. You do realize that you are just using a hideous kludge to emulate the behavior of the ‘goto’ keyword, right?

    ReplyDelete
  2. I’m not emulating THE ‘goto’ keyword which has many and various uses, but you could say that I am emulating A use of the ‘goto’ keyword.
    I rather simplified my use of the kludge, and will provide (later on today) a fuller example and let the reader judge which is most hideou; the kludge, or what it replaced,

    ReplyDelete