Using two values for one switch case statement

前端 未结 12 1775
梦谈多话
梦谈多话 2020-11-28 17:56

In my code, the program does something depending on the text entered by the user. My code looks like:

switch (name) {
        case text1: {
            //bla         


        
12条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 18:41

    The brackets are unnecessary. Just do

    case text1:
    case text4:
      doSomethingHere();
      break;
    case text2:
      doSomethingElse()
      break;
    

    If anyone is curious, this is called a case fallthrough. The ability to do this is the reason why break; is necessary to end case statements. For more information, see the wikipedia article http://en.wikipedia.org/wiki/Switch_statement.

提交回复
热议问题