通过假环跳绳开关的情况下是有效的操作? [英] Skipping switch cases via false loop is a valid operation?

查看:196
本文介绍了通过假环跳绳开关的情况下是有效的操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这会是合法code或违反任何规则?

Would this be legal code or breaking any rules?

switch (expr)
{
    do
    {
        case 6:
            /*...*/

            if (/*...*/)
                break;
        case 7:
            /*...*/

    } while (0);    

    case 9:
        /*...*/

        break;

    default:
        break;
}

请问这是只有满足一定条件的情况下执行6其次是案件7而是一种合法的方式?
或将这项领先进入不确定的行为,并让鼻龙走出开关?
附:我的问题是指的是C99。

Would this be a legal way of executing case 6 followed by case 7 but only if some conditions are met? Or would this lead into undefined behavior and lets nasal dragons come out of the switch? p.s. my Question is refering to c99.

编辑:

我想要做的是以下几点:
假设,外壳9,在everycase被执行。如果expr是6,我必须执行6,在某些情况下,7和9之后,或者如果expr为7的7-> 9
所以我只是想跳过7,如果某些条件得到满足,但我不能改变的6,7,9的顺序。

What i want to do is the following: assume, case 9, has to be executed in everycase. If expr is 6, i have to execute 6, under some conditions 7 and after that 9 or if expr is 7 its 7->9 so i just want to skip 7 if some conditions are met, but i cant change the order of 6,7,9.

EDIT2:

我不是在寻找替代soloution,我在这个片段的行为只是有兴趣。

I'm not looking for an alternative soloution, I'm just interested in the behavior of this snippet.

如果一个switch语句有一个范围内的相关案例或默认标签
  具有可变类型的标识符,整个switch语句须在
  该标识符的范围。

If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.

从ISO / IEC 9899:TC3 6.8.4.2-> 3

from ISO/IEC 9899:TC3 6.8.4.2->3

让我感到不确定它的行为。
但我不知道这将目标code像我这样的片断了。

Lets me feel unsure about its behavior. But I'm not sure that would aim code like mine snippet too.

推荐答案

也许我是那些糟糕的程序员之一。我不知道有关标准,但在需要时我一直在做这样的事情。这就是说,如果我想要做什么,你在你的问题已经指出,我会做这样的事情:

Maybe I'm one of those lousy programmers. I'm not sure about the standards, but I've been doing things like that when it's needed. That said, if I wanted to do what you have stated in your question, I'd do something like this:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv)
{
    int iCnt, iTmp;

    for(iCnt = 1; argc > iCnt; iCnt ++)
    {
        iTmp = (int) strtol(argv[iCnt], NULL, 10);

        switch(iTmp)
        {
            case 6:
                printf("Case 6\n");

            /* change this condition to whatever suits you */
            if (argc - 1 == iCnt)
            {
            case 7:
                printf("Case 7\n");
            }

            case 9:
                printf("Case 9\n");
            default:
                printf("Default case\n");
        }

        printf("\n");
    }

    return 0;
}

即。 if语句是更内在这里IMO。

i.e. an if statement is more intrinsic here IMO.

这篇关于通过假环跳绳开关的情况下是有效的操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆