problems/risks due to programming language, stories requested

Erland Sommarskog sommar at enea.se
Wed Mar 14 08:11:18 AEST 1990


I tried thrice mailing this guy, including the path(!) he gave
in his signature. Couldn't IBM afford to be better connected?

Tony Sanders (sanders at sanders.austin.ibm.com) writes:
>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.

Cross you heart, how often in practical programming do you
write such code?  And how often compared to normal switch
statements where an easily elided break would introduce
a simple bug? It might be that I never program in C, but I 
have never felt the need for a fall-through. Also, it seem more
frequent that I first want to execute some common code and
then split the cases further. In this case C's fall-throughs
help you none.

In Ada (please note, it's not all-capital) I would probably
have written the above as:

    IF N = 0 THEN
       Count := Count + 1;
    END IF;
    IF N IN 0..1 THEN
       OCount := OCount + 1;
    END IF;
    IF N IN 0..2 THEN
       Put(Count, 10);
       Put(OCount, 10);
       New_line;
    ELSE
       Put_line("Unknown.");
    END IF;

With a one-line statement for N = 2, I might have chosen to
repeat some lines of code, if the CASE statement better had
illustrated the problem. (And I would probably have made the
same arrangements in C.)
-- 
Erland Sommarskog - ENEA Data, Stockholm - sommar at enea.se



More information about the Comp.lang.c mailing list