Use a 'goto' in a switch?

前端 未结 7 1364
别跟我提以往
别跟我提以往 2020-12-03 00:25

I\'ve seen a suggested coding standard that reads Never use goto unless in a switch statement fall-through.

I don\'t follow. What exactly would this \'

7条回答
  •  鱼传尺愫
    2020-12-03 00:56

    In addition to using goto case, you can goto a label that is in another case clause:

        switch(i) {
        case "0":
            // do some stuff
            break;
        case "1":
            // other stuff, then "fall through" to next case clause
            goto Case2;
        case "2":
        Case2:
            break;
        }
    

    This way, you can jump to another case clause without worrying about the value or type of the expression.

    Some sort of explicit "fallthrough" keyword that can be substituted for break would have been nice, though...

提交回复
热议问题