Does case-switch work like this?

Today I came across a piece of code for the case, and was a little surprised to see how it works. The code was:

switch (blah)
{
case a:
  break;
case b:
  break;
case c:
case d:
case e: 
  {
    /* code here */
  }
  break;
default :
  return;
}

To my surprise, in the scenario where the variable was c, the path fell into the "here" segment. I agree that there is no end at the end cof the case switch part, but I would suggest that it goes through default. When you land on a line case blah:, it doesn’t check if the current value matches a specific case and only then in a specific segment? Otherwise, what's the point of dealing?

+5
source share
5 answers

. .

:

switch(blah)
{
case a:
  function1();
case b:
  function2();
case c:
  function3();
  break;
default:
  break;
}

blah == a, function1(), function2() function3().

, , break.

switch(blah)
{
case a:
  function1();
  break;
case b:
  function2();
  break;
case c:
  function3();
  break;
default:
  break;
}

switch, ( ) goto, case, . break, .

+13

, "". , . , .

:

switch(command)
{
   case CMD_SAVEAS:
   {
      this->PromptForFilename();
   } // DO NOT BREAK, we still want to save
   case CMD_SAVE:
   {
      this->Save();
   } break;


   case CMD_CLOSE:
   {
      this->Close();
   } break;

   default:
      break;
}
+10

.

, : .

:

// psuedo code:
void stopServer() {
    switch (serverStatus)
    case STARTING:
    {
        extraCleanUpForStartingServer();
        // fall-thru
    }
    case STARTED:
    {
        deallocateResources();
        serverStatus = STOPPED;
        break;
    }
    case STOPPING:
    case STOPPED:
    default:
        // ignored
        break;
}

. , deallocateResources STOPPED, STARTING . , " " STARTING.

STOPPED, STOPPING , ( ).

, , .

+4

, ++ : -)

"goto", switch(blah) " " , .

+2
source

In fact, the switch statement works the way you noticed it. It is designed in such a way that you can combine several cases together until a break occurs and it acts like a sieve.

Here is a real world example from one of my projects:

struct keystore_entry *new_keystore(p_rsd_t rsd, enum keystore_entry_type type, const void *value, size_t size) {
        struct keystore_entry *e;
        e = rsd_malloc(rsd, sizeof(struct keystore_entry));
        if ( !e ) 
                return NULL;
        e->type = type;
        switch (e->type) {
        case KE_DOUBLE:
                memcpy(&e->dblval, value, sizeof(double));
                break;
        case KE_INTEGER:
                memcpy(&e->intval, value, sizeof(int));
                break;

        /* NOTICE HERE */

        case KE_STRING:
                if ( size == 0 ) { 
                        /* calculate the size if it zero */
                        size = strlen((const char *)value);
                }
        case KE_VOIDPTR:
                e->ptr = rsd_malloc(rsd, size);
                e->size = size;
                memcpy(e->ptr, value, size);
                break;

        /* TO HERE */
        default:
                return NULL;
        }
        return e;
}

Code for cases KE_STRINGand KE_VOIDPTRis identical except in the case of calculating the size of the line.

+2
source

All Articles