What should be added to C

Richard Harter g-rh at cca.UUCP
Fri May 30 16:04:29 AEST 1986


In article <> greg at utcsri.UUCP (Gregory Smith) writes:
>In article <1497 at mmintl.UUCP> franka at mmintl.UUCP (Frank Adams) writes:
>
>>>> o Any sort of multi-level break statement.  There is no syntacticly clean
>>>> way of adding this to C.
>
>I don't agree.   what about
>statement ::=	break ;
>	|	break constant_exp ;
>
>e.g.	break 2;
>would break the current loop *and* the enclosing one.
>
	This is a chestnut.  'break n' type constructs have been
tried and found wanting.  The problem is that it can make code
maintenance messy -- any time you modify the code by adding a
level of blocking you have to modify all enclosed break statements.
Miss just one and its mystery bug time.

	The reason that multilevel breaks are a problem in C is
as follows.  There are two ways in which labels can be used, (a)
as the name of a block, and (b) as a transfer address.  Functions
use labels to name blocks (the body of the function).  Goto's use
labels as transfer addresses.  The clean way to implement multilevel
breaks is to use labelled blocks and name the block you are escaping
in the break statement.  However labels within function bodies as
transfer addresses.  You have a conflict of usage here.  Of course
you could add a block keyword and an escape keyword, e.g.

block a {
  ....
  block b {
    ....
    escape a;
    }
  ....
  }
next_statement_after_a;

The escape statement transfers control to the next statement after
block labelled a.  Fine, so far, looks clean, is clean.  But are you
sure you know where that statement is?  Maybe what you want is this:

begin a
  ....
  begin b
    ....
    escape a;
    end b
  ....
  end a

where labelled begins and ends delimit blocks.  Does this (you should
excuse the expression) begin to sound familiar, PL/I fans?  Personally
I think this approach has its points, but it's starting to stray a
fair bit from C.

		Richard Harter, SMDS Inc.



More information about the Comp.lang.c mailing list