c++ - 是否可以将 goto 与 switch 一起使用?

标签 c++ switch-statement goto

C# 似乎可以,但我需要 C++,最好是跨平台。

基本上,我有一个开关,可以根据单个条件对内容进行排序,然后在其他所有内容上回退到默认处理。

说:

switch(color)
{
case GREEN:
case RED:
case BLUE:
    Paint();
    break;
case YELLOW:
    if(AlsoHasCriteriaX)
        Paint();
    else
        goto default;
    break;
default:
    Print("Ugly color, no paint.")
    break;
}

最佳答案

艾哈迈德的回答很好,但也有:

switch(color)
case YELLOW:
    if(AlsoHasCriteriaX)
case GREEN:
case RED:
case BLUE:
        Paint();
    else
default:
        Print("Ugly color, no paint.");

人们往往会忘记开关的强大功能

关于c++ - 是否可以将 goto 与 switch 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8202199/

相关文章:

c++ - 为什么 switch 和 if 语句与转换运算符的行为不同?

bash - 如果返回代码 >=1 重新运行脚本,从头开始

c++ - auto_ptr 和 unique_ptr 有什么意义?

c++ - 在 VS2010 中用 wstring 替换 TR1 regex_replace?

c# - C# 8 新 switch 可以替换包含多个的代码块吗? : ? : expressions?

python - Python中有标签/转到吗?

c++ - 'goto *foo' 其中 foo 不是指针。这是什么?

c++ - 为各种 C/C++ 编译器优化代码

C++ WinSock2 : WSA_INVALID_HANDLE on connect() call

javascript - es6 中 case 后的 switch 语句中的大括号有什么作用?