Jumping from one case to the default case in switch statement

前端 未结 9 909
予麋鹿
予麋鹿 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:04

    Here's what I did:

    char ucResult = 1;
    switch(ch){
          case 'a':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case 'b':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case 'c':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          case '_':
              if(ucResult){
                 // do something
                 if(error) ucResult = 0;
              }
          default:
                 //
                 break;
    }
    

    With this structure, you can switch to default case from any previous cases. Handy for breaking outer loops too.

提交回复
热议问题