switch 语句中从一种情况跳转到默认情况

发布于 2024-12-05 15:24:21 字数 512 浏览 0 评论 0原文

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

在像上面这样的 switch 语句中,我输入 case 'a',只有当其中的条件发生时我才会中断,否则我想跳转到默认情况。除了标签或 goto 之外,还有其他方法可以做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

拿命拼未来 2024-12-12 15:24:21

goto 为了胜利

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

goto For The Win

switch (ch) {
    case 'a':
        if (1) goto LINE96532;
        break;
    case 'b':
        if (1) goto LINE96532;
        break;
LINE96532:
    default:
        //
        break;
}
任性一次 2024-12-12 15:24:21

只需重新排序案例,使该案例成为最后一个:

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

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;
}
何以心动 2024-12-12 15:24:21

如果条件不取决于情况,为什么要把它放在里面呢?

if (!condition){
  // do default
}else{
  switch(ch){
    case 'a':
      // do a
      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;
    ...
  }
}
初懵 2024-12-12 15:24:21

重构代码:

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
}

这还增加了灵活测试多个默认处理类的好处。假设您有一组共享一些共同逻辑的字符 [c, d, e, f]。对于这些情况,只需从 test_char() 返回 2(可能在测试了某些条件之后),并将 case 2: 处理程序添加到默认处理 switch 语句中。

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.

孤蝉 2024-12-12 15:24:21

我不确定这是否是最好的答案,但事情是这样的:

如果您绝对不想使用标签,并且希望将案例保持在当前顺序,那么您可以在案例“a”之后继续,然后check so see if(ch != 'a') 在每个后续情况的开头,仅在条件为真时执行语句:

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

这可能不是解决问题的最有效方法,但它应该完成您想要的。

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.

酒解孤独 2024-12-12 15:24:21

如果您必须首先使用 switch 语句,因为您要检查的条件取决于大小写(或者必须先评估大小写,然后才能检查条件),只需在 switch 情况下,如果设置了该标志,则执行默认操作。例如:

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
}

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
}
谁的年少不轻狂 2024-12-12 15:24:21

这就是我所做的:

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

使用此结构,您可以从任何以前的情况切换到默认情况。也可以方便地打破外循环。

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.

世界如花海般美丽 2024-12-12 15:24:21

我希望我的解决方案能回答您的问题。只需让案例一直执行(从匹配的案例开始),但使用条件来禁止后续案例运行。

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.

    }   
}

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.

    }   
}
断肠人 2024-12-12 15:24:21

好吧,帖子确实很老了,但还是回答一下大家:
你可以简单地写“goto default;”你会直接跳转到默认情况,没有任何问题。

例子:

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

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;
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文