Jumping from one case to the default case in switch statement

让人想犯罪 __ 提交于 2019-12-18 18:47:14

问题


switch(ch){
          case 'a':
                 //do something, condition does not match so go to default case
                 //don'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?


回答1:


goto For The Win

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



回答2:


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



回答3:


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

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



回答4:


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.




回答5:


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.




回答6:


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
}



回答7:


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.




回答8:


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.

    }   
}



回答9:


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



回答10:


jumping to default

  • use an empty case label just above default ( with an obvious name )
  • Use goto case "case-labelname" to jump to and then fall thru to default..

example

    switch (VAR)
{
    case "a":
        // dO STUFF !
    if(COND)
            goto case "DFLT";
        // dO STUFF !
        break;
    case "B":
        // dO STUFF !
        break;
    case "C":
        // dO STUFF !
    IF(COND)
            goto case "DFLT";
        break;
    case "DFLT":
    default:

       // dO DEFAULT
        break;
}


来源:https://stackoverflow.com/questions/7484660/jumping-from-one-case-to-the-default-case-in-switch-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!