[net.lang.c] default

kimcm@diku.UUCP (Kim Chr. Madsen) (06/20/84)

Jack Jansen:

>       What does
>
>     switch(i) {
>        ...
>        switch(j) {
>            ...
>            default: ...
>        }
>    }
>


First of all it doesn't make sense to just put a switch into a
switch statement, because when you enter the first switch
statement, you just jump to the corresponding case-label and if
noone matches then you jump to the default-label, in the
outermost switch-level. You can see the default-label in each
level as a local label, not known to the switch statements that
surrounds the actual switch label.

When I say that it doen't make sense to just put a switch
statement into another, it's a truth with modifications:

        a) if it has the form:

                switch (a) {
                case 1 : ...;
                         break;
                case 2 : switch (b) {
                         case 1 : ...;
                                  break;
                         default: ...;
                                  break;
                         }
                         break;
                default: ...;
                         break;
                }
           it certainly can be usefull as a control statement.

        b) But if you on the other hand construct a switch-
           statement like this:

                switch (a) {
                    switch (b) {
                        :
                        :
                    }
                }
           The switch-statements that lies within the outermost
           switch-statement will never be executed. [sic!]

If you want to see it for yourself then try to run this little
program:

        main ()
        {
            int i = 4,j = 5;

            switch (i) {
                switch (j) {
                case 7 : printf ("something wrong with j!\n");
                         break;
                default: printf ("j = %d (5)\n",j);
                         break;
                }
            case 8 : printf ("something wrong with i!\n");
                     break;
            default: printf ("i = %d (4)\n",i);
                     break;
            }
        }

>  Or I could even add a 'goto default'.........
>  (Note: my C Ref Man doesn't say that default is a reserved word,
>  so using default as an ordinary label is perfectly legal)

No you couldn't because default is a reserved word, so either
is your C Ref Man corrupted, or you maybe had overlooked the
section where the keywords is listed (-: However If your C Ref
Man does miss this section I have printed it below...

                     C Reference Manual


                     Dennis M. Ritchie

                     Bell Laboratories
               Murray Hill, New Jersey 07974

                        May 1, 1977

2.3 Keywords

The following identifiers are reserved for use as  keywords,
and may not be used otherwise:


             int       extern    else
             char      register  for
             float     typedef   do
             double    static    while
             struct    goto      switch
             union     return    case
             long      sizeof    default
             short     break     entry
             unsigned  continue  auto
             if


The keyword entry is not currently implemented by any
compiler but is reserved for future use.  Some imple-
mentations also reserve the word fortran.

                                        Kim Chr. Madsen.
                                        Dept. of Computer Science
                                        University of Copenhagen
                                                Denmark.
------------------------------
UUCP: ...mcvax!diku!kimcm

keesan@bbncca.ARPA (Morris Keesan) (06/20/84)

--------------------------------------------
>   What does
>   
>        switch(i) {
>   	    ...
>   	    switch(j) {
>   	        ...
>   	        default: ...
>   	    }
>       }
>   
>       do if there is no case corresponding to i?
>   	    .
>   	    .
>   	    .
>   Or I could even add a 'goto default'.........
>   (Note: my C Ref Man doesn't say that default is a reserved word,
>   so using default as an ordinary label is perfectly legal)
>   
>   	    Jack Jansen, {philabs!decvax}!mcvax!vu44!jack

    Well, first off, you have a defective "C Ref Man", because the C Reference
Manual in K&R clearly lists "default" as a reserved word (section 2.3, Keywords,
page 180 of K&R).
    Secondly, what happens is clearly defined (section 9.7, Switch Statement):

	Any statement within the statement may be labeled with one or more
	case prefixes . . .

	There may also be at most one statement prefix of the form
		    default:

	If no case matches and if there is no default then none of the
	statements in the switch is executed.

The "default" in the example above is not a prefix of a "statement within the
statement", but a prefix of a statement within a statement within the statement.
The default will never be considered unless the switch(j) statement is being
executed.  The answer to the question is that the above fragment evaluates i,
compares its value to any case constants (assumed to be in the ...), and then
does nothing else.
-- 
					Morris M. Keesan
					{decvax,linus,wjh12,ima}!bbncca!keesan
					keesan @ BBN-UNIX.ARPA

duk@vu44.UUCP (Duk Bekema) (06/22/84)

In what place in the manual is stated that default (and case)
belongs to the nearest enclosing switch?
Of course, it is logical, it is exactly what I want, and I've
never thought otherwise; but I just can't find it.

This piece of program will print `Got here':

	switch (3) {
	default:
		if (0) {
		case 3:	printf("Got here\n");
			break;
		}
		break;
	}

but this piece won't:

	switch (3) {
	default:
		switch (0) {
		case 3:	printf("Got here\n");
			break;
		}
		break;
	}

				Duk Bekema
				..!mcvax!vu44!duk

jack@vu44.UUCP (Jack Jansen) (06/25/84)

This has probably been discussed before, as everything has,
but here goes :
What does

     switch(i) {
	...
	switch(j) {
	    ...
	    default: ...
	}
    }

    do if there is no case corresponding to i?
I can imagine even worse constructions like
    switch(i) {
	switch(j) {
	default:....
	}
	switch(k) {
	default:....
	}
    }
Or I could even add a 'goto default'.........
(Note: my C Ref Man doesn't say that default is a reserved word,
so using default as an ordinary label is perfectly legal)

	Jack Jansen, {philabs!decvax}!mcvax!vu44!jack