C ++: switch statement missing semicolon before closing bracket

In the interest of future readers and my own sanity, I would like to state very clearly that switchstatements that do not have a case default(due to all the cases considered) or are consistent if-elseif-elsewith the last that should not do anything should not be omitted and include a comment on this issue (see example).

However, whenever I include a case defaultin a statement switchand leave it empty, I have to put a semicolon inside the case defaultor a compiler error: "Line [Closing line of the parenthesis of the switch statement]` missing ';' before '}'". WHY?!

EXAMPLE: COMPUTER ERROR GENERATORS

switch(direction) {
    case MOVE_UP:
    //...
    break;
    case MOVE_RIGHT:
    //...
    break;
    case MOVE_DOWN:
    //...
    break;
    case MOVE_LEFT:
    //...
    break;
    default:
        /* DO NOTHING */
}

:

switch(direction) {
    case MOVE_UP:
    //...
    break;
    case MOVE_RIGHT:
    //...
    break;
    case MOVE_DOWN:
    //...
    break;
    case MOVE_LEFT:
    //...
    break;
    default:
        /* DO NOTHING */;
}
+3
2

6.1/1 ++ 03 :

labeled-statement:
    identifier : statement
    case constant-expression : statement
    default : statement

++ 11 , , .

++, default: -.

, , , default: statementopt. , , default : case 1: break;, case 1: break; , default:, default: , . , , , , , .

+9

default. - .

+12

All Articles