Switch statement inside a switch statement?

前端 未结 8 1797
傲寒
傲寒 2021-02-07 02:40

I have to evaluate many conditions. In my case, I have to do something like this:

switch(id)
{
    case 5:
         // switch some other cases here
    case 6:
          


        
8条回答
  •  迷失自我
    2021-02-07 03:14

    The only thing that could be wrong with it is that it could hurt readability:

    switch(id)
    {
        case 5:
        {
            switch (somethingElse)
            {
                case 1:
                    // blah...
            }
        }
        case 6:
             // set some value
        ...
    }
    

    You could improve this by moving the nested section into a method:

    switch(id)
    {
        case 5:
            Foo();
            break;
        case 6:
             // set some value
        ...
    }
    

提交回复
热议问题