Jumping from one case to the default case in switch statement

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

    I hope my solution answers your question. Simply let the cases follow through all the way (beginning with the matching case) but use a condition to disable subsequent cases from running.

    typedef enum boolean
    {
        FALSE = 0, TRUE = 1
    } bool;
    
    void pstuff(char input)
    {
        bool _skip = FALSE; 
        switch(input)
        {
            case 'a':
                printf("Case a.");
                _skip = TRUE; 
            case 'b': 
                if(!_skip)
                {
                    printf("Case b.");
                    _skip = TRUE;
                }
            case 'c':       
                if(!_skip)
                {
                    printf("Case c.");
                    _skip = TRUE; 
                }
            //...
            default: 
                printf("Done!\n"); //Always gets printed.
    
        }   
    }
    

提交回复
热议问题