开发者

Jumping from one case to the default case in switch statement

switch(ch){
          case 'a':
                 //do something, condition does not match so go to default case
                 //d开发者_运维百科on't break in here, and don't allow fall through to other cases.
          case 'b':
                 //..
          case 'c':
                 //..
          case '_':
                 //...
          default:
                 //
                 break;
}

In a switch statement like above one I enter case 'a', I break only if the condition inside it occurs, otherwise I want to jump to default case. Is there any other way of doing this rather than labels or gotos?


goto For The Win

switch (ch) {
    case 'a':
        if (1) goto LINE96532;
        break;
    case 'b':
        if (1) goto LINE96532;
        break;
LINE96532:
    default:
        //
        break;
}


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;
}


If the condition doesn't depend on cases, why put it inside?

if (!condition){
  // do default
}else{
  switch(ch){
    case 'a':
      // do a
      break;
    ...
  }
}


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.


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.


If you must have the switch statements first because the condition you're checking for depends on the case (or the case has to be evaluated first before you can check on the condition), simply set a flag inside your switch cases, and if that flag is set, then do a default operation. For instance:

int default_true = 0;
switch (case_value)
{
    case 'a': /* if the condition is true, set the default_true flag */

    case 'b': /* if the condition is true, set the default_true flag */

    //...

    default: default_flag = 1; // set the default_true flag to true
}

if (default_flag)
{
    //place your "default" code here rather than inside the switch statement
    //this prevents code reduplication
}


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.


I hope my solution answers your question. Simply let the cases follow through all the way (beginning with the matching case) but use a condition to disable subsequent cases from running.

typedef enum boolean
{
    FALSE = 0, TRUE = 1
} bool;

void pstuff(char input)
{
    bool _skip = FALSE; 
    switch(input)
    {
        case 'a':
            printf("Case a.");
            _skip = TRUE; 
        case 'b': 
            if(!_skip)
            {
                printf("Case b.");
                _skip = TRUE;
            }
        case 'c':       
            if(!_skip)
            {
                printf("Case c.");
                _skip = TRUE; 
            }
        //...
        default: 
            printf("Done!\n"); //Always gets printed.

    }   
}


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜