Jumping from one case to the default case in switch statement

前端 未结 9 905
予麋鹿
予麋鹿 2021-01-03 22:59
switch(ch){
          case \'a\':
                 //do something, condition does not match so go to default case
                 //don\'t break in here, and don\'t         


        
相关标签:
9条回答
  • 2021-01-03 23:00

    goto For The Win

    switch (ch) {
        case 'a':
            if (1) goto LINE96532;
            break;
        case 'b':
            if (1) goto LINE96532;
            break;
    LINE96532:
        default:
            //
            break;
    }
    
    0 讨论(0)
  • 2021-01-03 23:04

    Refactor your code:

    int test_char(char ch)
    {
      switch(ch) {
        case 'a': if (condition) return 0; break;
        case 'b': // ...
        default: return -1;
      }
    
      return 1;
    }
    
    ... 
    // defaults processing switch
    switch(test_char(ch)) {
      case 0: break; // condition met
      case 1: // default processing
      default: // case not handled by test_char
    }
    

    This also adds the benefit of being flexible to test for multiple classes of default processing. Say you have a group of chars [c, d, e, f] which share some common logic. Simply return 2 from test_char() for these cases (possibly after some conditions has been tested), and add a case 2: handler to the default processing switch statement.

    0 讨论(0)
  • 2021-01-03 23:04

    Here's what I did:

    char ucResult = 1;
    switch(ch){
          case 'a':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case 'b':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case 'c':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case '_':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          default:
                 //
                 break;
    }
    

    With this structure, you can switch to default case from any previous cases. Handy for breaking outer loops too.

    0 讨论(0)
  • 2021-01-03 23:10

    Well, the post is really old but to answer everyone: you can simple write 'goto default;' and you will directly jump to the default case without any problems.

    Example:

            switch (value)
            {
                case value1:
                   // do something;
                    break;
                case value2:
                   // do something
                    break;
               .
               .
               .
               .
                case value20:
                   // do something
                    **goto default;**
               .
               .
                case valueN:
                    // do something
                    break;
    
                default:
                    // do something
                    break;
            }
    
    0 讨论(0)
  • 2021-01-03 23:17

    I'm not sure if thes is the best answer, but here it goes:

    If you absolutely do not want to use labels, and you want to keep the cases in their current order, then you could continue after case 'a' and then check so see if(ch != 'a') at the beginning of each subsequent case, only executing the statement if the condition is true:

    switch(ch){
        case 'a':
            // do something
        case 'b':
        if(ch != 'a') {
            //do something
        }
        //repeat for each subsequent case
        default:
            //do something
        break;
    }
    

    This is probably not the most efficient way to solve your problem, but it should accomplish what you want.

    0 讨论(0)
  • 2021-01-03 23:20

    Just reorder the cases so that that case is the last:

    switch(ch){
              case 'b':
                     //..
              case 'c':
                     //..
              case '_':
                     //...
              case 'a':
                     //do something, condition does not match so go to default case
                     if (condition)
                         break;
                     //don't break in here, and don't allow fall through to other cases.
              default:
                     //
                     break;
    }
    
    0 讨论(0)
提交回复
热议问题