[comp.lang.c] The D Switch Loop??

ksb@j.cc.purdue.edu (Kevin Braunsdorf) (03/02/88)

For a replacement for C why can't we make a switch statement loop?  We
can make switch less of a hack in the lanuage.

Make the switch loop be default so we trap switches without a default.

	switch (func(param)) {
	case (0):
		param = dosomething();
		continue;
	case (1):
		param = tryagain();
		continue;
	case (2):
		param = stillanother();
		continue;
	case (3):
	default:
		break;
	}

This way we have a DFA.  Maybe we can even dink the ':' after the
case.

Kevin S Braunsdorf
ksb@s.cc.purdue.edu

karl@haddock.ISC.COM (Karl Heuer) (03/02/88)

In article <6548@j.cc.purdue.edu> ksb@s.cc.purdue.edu.UUCP (Kevin Braunsdorf) writes:
>For a replacement for C why can't we make a switch statement loop?  We
>can make switch less of a hack in the lanuage.

That's a step backwards.  `if` and `switch` are for selection, `for` and `do`
and `while` are for loops; I see no need for a construct that does both.

Given the existence of a proper `switch` statement (one in which the cases do
not fall through into each other), your example can be written more cleanly:
insert a `for (;;)` in front of the `switch`, and remove the `continue`
statements.  The `break`, no longer meaningful to `switch`, now exits the
`for` loop.

(Personally, I'd probably write it as a `while` and a `switch`, so I wouldn't
need the `break` either.  This would require a temporary for the `switch`
parameter, but one usually needs that anyway when handling `default`.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint