[comp.lang.misc] Case statements in sane languages

worley@Compass.COM (Dale Worley) (03/19/90)

    From: sanders@sanders.austin.ibm.com (Tony Sanders)

    How do you do this in ADA?

	switch(n) {
	  case 0:
	    count++;
	  case 1:
	    ocount++;
	  case 2:
	    printf("%d %d\n",count,ocount);
	    break;
	  default:
	    printf("unknown n\n");
	    break;
	}

    See how I left out the breaks on purpose.

    In ADA you wouldn't be able to do this without duplicating either the
    case-expression (they aren't always simple numbers) or the statements.

In this case, duplicating the statements wouldn't be hard enough to
worry about.  If the bodies of the cases were large enough to make it
hard, you can write a local procedure, and just call it from several
cases.  (Since Ada has nested procedures, you can write a procedure
that has access to local variables.)

    The 11th commandment: "Thou shalt use lint"

In Ada (or any sane language), "Lint" is called "the compiler".

Dale Worley		Compass, Inc.			worley@compass.com
--
Why are you RUNNING?  Cerebus just wants to KILL you a little...

mob@mit-amt.MEDIA.MIT.EDU (Mario O. Bourgoin) (03/20/90)

I feel I must interject a some Scheme code in this otherwise C-based
discussion cross-posted to comp.lang.scheme.  In Scheme, there's a
control construct called CASE which is much like C's SWITCH except
that doesn't allow the clauses to be cascaded but it does allow
multiple constants per clause.  It's easy to imagine a CASE-EVERY (in
the spirit of ZETALISP's COND-EVERY) very similar to CASE but that
evaluates every clause whose constant part includes the key.  The
resulting structure passes control more explicitly than its C
equivalent, and yet remains simple enough to encourage programmers to
use it.  For example:

	(case-every (0
		     (1+ count))
		    ((0 1)
		     (1+ ocount))
		    ((0 1 2)
		     (writeln count ocount))
		    (else
		     (writeln "unknown n")))

--Mario