Another "D" idea (Oh no, not again!)

terry terry at wsccs.UUCP
Sun Mar 20 20:28:20 AEST 1988


In article <316 at wsccs.UUCP>, val at wsccs.UUCP (Val Kartchner) writes:
>      I also think that there should be something like "break 'n';" (Where
>      'n' is a constant integer.  Variables would be interesting and floats
>      highly ambiguous.)  I've often been deep in multiple loops, and wanted
>      to get out a few levels.  Most of the time, I have all the loops that I
>      want to exit in a subroutine, and just return, but sometimes I can't
>      afford the time for the function call and the return.  This same time
>      critical restriction is for testing a "flag" variable at EVERY loop
>      condition; I can't always afford the time.

	Why not use a 'goto'?  This is precisely what it's there for...
discontinuous transfer of control.  A goto would eloquently avoid the
need to test a flag variable, as you would have to if you implimented all
your code with while()'s, and it usually compiles to a single machine
instruction (JMP #addr), thereby satisfying your time constraints.

[examples of 'break n' code deleted]

>      A similar difficulty could be bypassed in the "switch" statement if
>      this were allowed:

[a rather obscure for-loop-to-avoid-a-goto deleted]

Again, why not use a goto?  Is it the current indoctrination in computer
science classes to "avoid goto's at all costs, even if it increases code
complexity and execution time"?  The language feature is _there_ for handling
of abnormal conditions.

>      Remember, I ask this to increase the usability of C (C is my computer
>      language of choice), but it must be used with care.  It would tend to
>      disabiguize some complex looping structures, but it must not be
>      overused as "goto" often is.

There are several problems with this.  The first and foremost is that you are
suggesting something that is _not_ simplifying.  How is the need to count
braces to determine where you are going to end up simpler than an explict
labeling of where you want control to resume?  It seems to me that if your
code did this:

	while( 1) {
		switch( var) {
		case 1:
			...
			break;
		...
		default:
			if( cond)
				break 2;
			break;
		}
	}

and it changed (say you forgot to do something on the condition 'cond'), you
would have to be very careful to make sure you were breaking the approprite
number of levels... something which would have been automatic, had you used
a goto.

As an old 6502 assembly programmer, you should realized that you are simply
exchanging a jump for a jump relative... in other words, renaming the goto
and making the execution path more obscure, NOT making it clearer.


| Terry Lambert           UUCP: ...{ decvax, ihnp4 }                          |
| @ Century Software          : ...utah-cs!uplherc!sp7040!obie!wsccs!terry    |
| SLC, Utah                                                                   |
|                   These opinions are not my companies, but if you find them |
|                   useful, send a $20.00 donation to Brisbane Australia...   |
| 'There are monkey boys in the facility.  Do not be alarmed; you are secure' |



More information about the Comp.lang.c mailing list