Jumping from one case to the default case in switch statement

前端 未结 9 904
予麋鹿
予麋鹿 2021-01-03 22:59
switch(ch){
          case \'a\':
                 //do something, condition does not match so go to default case
                 //don\'t break in here, and don\'t         


        
9条回答
  •  清酒与你
    2021-01-03 23:17

    I'm not sure if thes is the best answer, but here it goes:

    If you absolutely do not want to use labels, and you want to keep the cases in their current order, then you could continue after case 'a' and then check so see if(ch != 'a') at the beginning of each subsequent case, only executing the statement if the condition is true:

    switch(ch){
        case 'a':
            // do something
        case 'b':
        if(ch != 'a') {
            //do something
        }
        //repeat for each subsequent case
        default:
            //do something
        break;
    }
    

    This is probably not the most efficient way to solve your problem, but it should accomplish what you want.

提交回复
热议问题