The Sins of K&R (break n)

Ravi K Mandava Vox Populi ravim at gtenmc.UUCP
Tue Oct 2 10:24:48 AEST 1990


In article <12252 at crdgw1.crd.ge.com> volpe at underdog.crd.ge.com (Christopher R Volpe) writes:
 >In article <4b0hPyW00Vp8M7qVd1 at andrew.cmu.edu>, jh4o+ at andrew.cmu.edu
 >(Jeffrey T. Hutzelman) writes:
 >|>volpe at underdog.crd.ge.com (Christopher R Volpe) writes
 >|>
 >|>> Ok, how about requiring the programmer to make it explicit when
 >|>> he/shewants it to fall through, rather than making that the default?
 >|>> Like so:
 >|>>
 >|>>switch(ch)
 >|>>     {
 >|>>     case 'a':
 >|>>        do_a();
 >|>>        continue;
 >|>>     case 'b':
 >|>>        do_a_or_b();
 >|>>     }
 >|>
 >|>No!!!  I LIKE being able to do the following:
 >[deleted example using continue statement]
 >
 >Sorry, I didn't mean to overload that keyword. I should have suggested
 >a new keyword, although I can't think of a good one right now...
 >                                              
 >==================
 >Chris Volpe


How about 'proceed'?

To solve the original problem (or sin, whatever) (i.e. to be able to break
out of a switch statement within a 'for' or 'while' loop), why not change
the 'break' statement to take an argument (like the 'return' statement does)
that denotes the number of loops (including switch body) that you would
like to get out of?
	    (If my memory serves me right, this syntax
		[ break n		- n is no of loops default being 1 ]
	     is implemented in 'sh' and 'ksh' languages.)

IMHO, this has some advantages like
	(a) you can still use the old syntax (like having to use 'break'
	    in each 'case' body thus allowing 'fall through' wherever it
	    is useful and having the ability to break out of the innermost
	    'for' or 'while' loop)
	(b) you can break out of any number of loops thus eliminating
	    the need of using flags

The same syntax can be extented to the 'continue' statement as well.


	-	Ravi Mandava



More information about the Comp.lang.c mailing list