Re: [OFF-TOPIC] question about switch, return, break

From: Daniel Koepke (dkoepke@CALIFORNIA.COM)
Date: 12/19/97


Sean Butler wrote:
>
> >>
> >> switch (mode) {
> >>   case SCMD_DROP:
> >>     obj_to_room(obj, ch->in_room);
> >>     return 0;
> >>     break;
> >>   case SCMD_DONATE:

> I don't think this is true:
>
>         char c;
>
>         c = get_a_char();
>
>         switch (c) {
>           case 'a':
>                 printf("case a");
>           case 'b':
>                 printf("case b");
>           case 'c':
>                 printf("case c");
>           default:
>                 printf("always print this");
>         }

Take notice that there is a *big* difference between,

  switch (i) {
  case 'a':
    return 0;
  default:
    printf("Hit default.\r\n");
    break;
  }

and

  switch (i) {
  case 'a':
    printf("case a");
  default:
    printf("default");
  }

At least one 'break' statement is required.  In other words, any
opening 'case', MUST be terminated by a 'break' statement.  This is
allowable:

  switch (i) {
  case 'a':
    return 0;
  default:
    break;
  }

but, this isn't:

  switch (i) {
  case 'a':
    break;
  default:
    return 0;
  }


> break is not needed in a switch statement.

That is absolutely, unequivacally *NOT* true for ANSI C compatible
compilers.  'break' is required.  If you want to test it, just
type in a switch() statement that has no 'break's and see what the
compiler tells you.


daniel koepke / dkoepke@california.com


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST