Path: utzoo!utgpu!news-server.csri.toronto.edu!mailrus!cs.utexas.edu!tut.cis.ohio-state.edu!ucbvax!agate!purina!raymond From: raymond@purina.berkeley.edu (Raymond Chen) Newsgroups: comp.lang.c Subject: Re: problems/risks due to programming language, stories requ Message-ID: <1990Mar15.205909.7882@agate.berkeley.edu> Date: 15 Mar 90 20:59:09 GMT References: <1306@mindlink.UUCP> Sender: usenet@agate.berkeley.edu (USENET Administrator;;;;ZU44) Reply-To: raymond@math.berkeley.edu (Raymond Chen) Organization: U.C. Berkeley Lines: 46 In article <1306@mindlink.UUCP> a563@mindlink.UUCP (Dave Kirsch) writes: >C flaws? Do this in Pascal: > >switch (i) { > case 2 : /* Add 2 to j */ > j++; > case 1 : /* Add 1 to j */ > j++; > case 0 : /* Print j out */ > printf("%d\n", j); > break; > default : > printf("Illegal value in switch.\n"); > abort(); >} Sure case i of 2 : begin j := j + 1; goto 1; end; 1 : begin 1: j := j + 1; goto 0; end; 0 : begin 0: writeln(j); end; else begin writeln('Illegal value in case.'); Halt; end; end; [It's been some time since I last programmed in Pascal, so I may have misplaced a semicolon or two.] Remember: C's switch statement is a thinly-disguised computed goto. If you're going to use goto's you may as well use them clearly and explicitly. I never use fallthrough%. If I need fallthrough, I make the fallthrough EXPLICIT via goto's. That way EVERYBODY knows what I'm doing: You, me, and lint. And of course it incurs absolutely NO performance penalty. (The compiler can't cache register values across "case" labels since they are just labels for the computed goto.) The only possible penalty is from a compiler which lacks a peephole optimizer. (You just have to elide the redundant jump statement.) -- % Well, rarely. I'll use fallthrough when writing my entries into the International Obfuscated C Code Contest.