Using continue in a switch statement

后端 未结 7 508
盖世英雄少女心
盖世英雄少女心 2020-12-13 03:29

I want to jump from the middle of a switch statement, to the loop statement in the following code:

while (something = get_something())
{
    swi         


        
7条回答
  •  無奈伤痛
    2020-12-13 03:46

    It's syntactically correct and stylistically okay.

    Good style requires every case: statement should end with one of the following:

     break;
     continue;
     return (x);
     exit (x);
     throw (x);
     //fallthrough
    

    Additionally, following case (x): immediately with

     case (y):
     default:
    

    is permissible - bundling several cases that have exactly the same effect.

    Anything else is suspected to be a mistake, just like if(a=4){...} Of course you need enclosing loop (while, for, do...while) for continue to work. It won't loop back to case() alone. But a construct like:

    while(record = getNewRecord())
    {
        switch(record.type)
        {
            case RECORD_TYPE_...;
                ...
            break;
            default: //unknown type
                continue; //skip processing this record altogether.
        }
        //...more processing...
    }
    

    ...is okay.

提交回复
热议问题